R

Asked • 09/07/19

Paste multiple columns together?

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?

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.