Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste multiple columns together

Tags:

r

r-faq

paste

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?

like image 667
user1165199 Avatar asked Jan 28 '13 18:01

user1165199


People also ask

How do I paste multiple columns into one in R?

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>.

How do I copy and paste columns in R?

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.

How do I stack two columns in R?

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.


2 Answers

# 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 ) ] 
like image 103
Anthony Damico Avatar answered Sep 20 '22 22:09

Anthony Damico


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 
like image 25
Brian Diggs Avatar answered Sep 17 '22 22:09

Brian Diggs