0 votes
1 view
in R Programming by (5.2k points)

I have a bunch of columns in a data frame which I want to paste together (separated 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?

1 Answer

0 votes
by (25.3k points)

To paste multiple columns together, you can do the following:

# 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 ) ]

 data

  a     x

1 1 a-d-g

2 2 b-e-h

3 3 c-f-i

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...