Paste multiple columns together

user1165199 picture user1165199 · Jan 28, 2013 · Viewed 114.4k times · Source

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?

Answer

Anthony Damico picture Anthony Damico · Jan 28, 2013
# 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 ) ]