I have a bunch of columns in a dataframe which I want to paste together (seperated by "-") as follows:
data <- data.frame('a' = 1:3, 'b' = c('a','b','c'), 'c' = c('d', 'e', 'f'), 'd' = c('g', 'h', 'i')) i.e. a b c d 1 a d g 2 b e h 3 c f i
Which I want to become:
a x 1 a-d-g 2 b-e-h 3 c-f-i
I could normally do this with:
within(data, x <- paste(b,c,d,sep='-'))
and then removing the old columns, but unfortunately I do not know the names of the columns specifically, only a collective name for all of the columns, e.g. I would know that cols <- c('b','c','d')
Does anyone know a way of doing this?
How do I concatenate two columns in R? To concatenate two columns you can use the <code>paste()</code> function. For example, if you want to combine the two columns A and B in the dataframe df you can use the following code: <code>df['AB'] <- paste(df$A, df$B)</code>.
The best way to replicate columns in R is by using the CBIND() function and the REP() function. First, you use the REP() function to select a column and create one or more copies. Then, you use the CBIND() function to merge the original dataset and the replicated columns into a single data frame.
Method 1: Using stack method The cbind() operation is used to stack the columns of the data frame together. Initially, the first two columns of the data frame are combined together using the df[1:2]. This is followed by the application of stack() method applied on the last two columns.
# your starting data.. data <- data.frame('a' = 1:3, 'b' = c('a','b','c'), 'c' = c('d', 'e', 'f'), 'd' = c('g', 'h', 'i')) # columns to paste together cols <- c( 'b' , 'c' , 'd' ) # create a new column `x` with the three columns collapsed together data$x <- apply( data[ , cols ] , 1 , paste , collapse = "-" ) # remove the unnecessary columns data <- data[ , !( names( data ) %in% cols ) ]
As a variant on baptiste's answer, with data
defined as you have and the columns that you want to put together defined in cols
cols <- c("b", "c", "d")
You can add the new column to data
and delete the old ones with
data$x <- do.call(paste, c(data[cols], sep="-")) for (co in cols) data[co] <- NULL
which gives
> data a x 1 1 a-d-g 2 2 b-e-h 3 3 c-f-i
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With