Prefix/suffix column content with column names

Prefix/suffix column content with column names

Content Index :

Prefix/suffix column content with column names
Tag : r , By : nemof
Date : November 24 2020, 04:01 AM

may help you . We can use imap, which would also include the column names of each column if we need to extract. Here, we paste the column names (.y) with the column values (.x)
imap(x, ~ paste0(.y, .x)) %>%
# A tibble: 4 x 2
#  VarX  VarY 
#   <chr> <chr>
#1 VarXA VarYC
#2 VarXB VarYD
#3 VarXA VarYC
#4 VarXB VarYD
imap_dfc(x, ~ paste0(.y, .x))
x[] <- paste0(rep(colnames(x), each = nrow(x)), as.matrix(x))
#   VarX  VarY
#1 VarXA VarYC
#2 VarXB VarYD
#3 VarXA VarYC
#4 VarXB VarYD

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How to prefix / suffix column names in a MySQL query?

Tag : php , By : n1ckless_id
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I'm storing the user type (seller or buyer) in one of my session variables as $_SESSION['user_type']. , Just concatenate query as string and use it then.
$query = "SELECT *
    FROM `order`
    WHERE " . $_SESSION['user_type'] . "_id = '7'";

Add repeated-measures column names as prefix instead of suffix using reshape

Tag : r , By : user181945
Date : March 29 2020, 07:55 AM
To fix this issue I'm not sure whether you can change the order within reshape, but you can change it afterwards using gsub with a Regular Expression:
names(mReshaped) = gsub("(Q[0-9])(.*)", "\\2\\1", names(mReshaped))

 [1] "Participant"    "FirstSurveyQ1"  "FirstSurveyQ2"  "FirstSurveyQ3"  "SecondSurveyQ1"
 [6] "SecondSurveyQ2" "SecondSurveyQ3" "ThirdSurveyQ1"  "ThirdSurveyQ2"  "ThirdSurveyQ3" 
[11] "FourthSurveyQ1" "FourthSurveyQ2" "FourthSurveyQ3"

How to add a suffix (or prefix) to each column name?

Tag : python , By : Menno
Date : March 29 2020, 07:55 AM
help you fix your problem I want to add _x suffix to each column name like so: , You can use a list comprehension:
df.columns = [str(col) + '_x' for col in df.columns]

Transform suffix into prefix in column name

Tag : r , By : Dasharath Yadav
Date : March 29 2020, 07:55 AM
it helps some times I would like to move the suffix of a column name to its beginning so that it becomes its prefix. I have many columns with changing names (except the suffix), so manually renaming is not an option. , A couple of gsubs and paste0 will do the trick:
gsub("y$", "x", gsub("(^.*)\\.(.*a$)", paste0("\\2", ".", "\\1"), names(dat)))
[1] "ID"            "alpha.speed.x" "alpha.power.x" "alpha.force.x" "beta.speed.x" 
[6] "beta.power.x"  "beta.force.x"
names(dat) <- gsub("y$", "x", gsub("(^.*)\\.(.*a$)", paste0("\\2", ".", "\\1"), names(dat)))

Rename all column names with a suffix except listed column name using dplyr?

Tag : r , By : Ivan Kitanovski
Date : March 29 2020, 07:55 AM
Does that help I'm hoping this is a simple question. How do I rename all of the column header names in a data set with the suffix "_2017" except the first 2 column headers which are: "Name", and "State"? I'd like to use dplyr to do this. , You can use rename_at and exclude columns using vars helper method:
df <- data.frame(Name = c('a', 'b'), State = c('c', 'd'), col1 = 1:2, col2 = 3:4)
#  Name State col1 col2
#1    a     c    1    3
#2    b     d    2    4
df %>% rename_at(vars(-Name, -State), ~ paste0(., '_2017'))
#  Name State col1_2017 col2_2017
#1    a     c         1         3
#2    b     d         2         4
df %>% rename_at(vars(-(1:2)), ~ paste0(., '_2017'))
#  Name State col1_2017 col2_2017
#1    a     c         1         3
#2    b     d         2         4
to_exclude = c('Name', 'State')
df %>% rename_at(vars(-one_of(to_exclude)), ~ paste0(., '_2017'))
#  Name State col1_2017 col2_2017
#1    a     c         1         3
#2    b     d         2         4
Related Posts Related QUESTIONS :
  • Remove a single backslash in paste0 output
  • ggplot2 different label for the first break
  • TSP in R, with given distances
  • How to find the given value from the range of values?
  • Solution on R group by issue _ multiple combination
  • Transform multiple columns with a function that uses different arguments per column
  • How can I parse a string with the format "1/16/2019 1:24:51" into a POSIXct or other date variable?
  • How to plot a box plot in R for outlier detection for a huge number of rows?
  • How to change column name according to another dataframe in R?
  • `sjPlot::tab_df()`--how to set the number of decimal places?
  • time average for specific time range in r
  • joining dataframes by closest time and another key in r
  • How to create nested for loop for a certain range
  • New category based on sequence of date ranges
  • how to extract formula from coxph model summary in R?
  • add row based on variable condition in R
  • Generating the sequence 111122222333334
  • Unable to use has_goog_key() in R
  • how to multiply each row with a scaler in corresponding column?
  • R is not recognizing levels of a factor as the same. Is there a way to do this?
  • Calculating mean of replicate experiment result values in a column based on multiple columns using R
  • Best method to extract the first instance of a string between specified keywords using data.table
  • ignore optional combination of alphanumeric characters in str_extract
  • Why tracemem shows two copies when modification occurs inside function body?
  • Can't use mppm on multitype point patterns
  • How to move selected matrix rows to top of matrix based on a selection vector of row names
  • Combining expressions with a common operator
  • Passing string through multiple filters for matching
  • Convert two columns in R to rows of unique occurrence
  • How to create a dataframe using a function based on user-input?
  • How to access the visited vertices in a given shortest path using R igraph
  • Differences in Unicode character output with print()
  • Extracting Function or Objects from a String and then Piping Them with Magrittr/Dplyr
  • renderUI not evaluated until it is rendered
  • Find the maximum absolute value by row in an R data frame
  • Extracting data from irregular lists using purrr:map()
  • transforming data based on range of column in r
  • Identify and subset rows with some similar information
  • converting character from mongolite to timestamp in R
  • Create list from two vectors with every combo of each
  • Error in running a spread because of unique 'key combinations'; combining rows of data
  • visualize numerical strings as a matrixed heatmap
  • how to make a blocked matrix?
  • How to summarize with two functions using with dplyr
  • Dataframe is no longer the same after being saved to Excel and read back in
  • Create duplicate rows using based on availability of data
  • Keep empty groups when grouping with data.table in R
  • Grouping of Event Time Data based on multiple, iterative conditions
  • Formatting Numbers in Flextable for Specific Columns
  • How to store results from for-loop into a dataframe
  • How to select the values in my dataframe which has logical operator "<" (less than), divide them by two, an
  • Rowwise extract data between two strings
  • Convert a string separate by . and +
  • stacking function for values in R
  • dplyr coerces characters to factors
  • How do I use spread and group_by on a single row dataset
  • Replacing values in one matrix with values from another
  • Aggregate data and exclude duplicates in one column
  • Perform an R data.table binary search with OR select
  • How can I include a function in the Standard Deviation parameter of pnorm
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com