0

Suppose I have a dataframe:

require(dplyr)
df <- data.frame(COL1 = c("a1 a2 a3", "b1 b2 b3", "c1 c2 c3"), 
                 COL2 = c("x", "y", "z"),
                 COL3 = c("1", "2", "3"))

I want the result to be

  COL1_1 COL1_2 COL1_3 COL2 COL3
1     a1     a2     a3    x    1
2     b1     b2     b3    y    2
3     c1     c2     c3    z    3

What I have right now to do so is

df %>%
  separate(col = COL1, sep = " ", into = c("COL1_1", "COL1_2", "COL1_3"))

Is there a way to automate the into part of the expression?

Something like that uses the original column name COL1 and concatenates that with a subscript _i with i being the the "subcolumn". The approach needs to adapt automatically to different numbers of "subcolumns", e.g. a1, a2, a3,..., an.

Boern
  • 7,233
  • 5
  • 55
  • 86
  • We can do this with `read.table` i.e. `cbind(read.table(text=as.character(df$COL1)), df)` – akrun Oct 15 '15 at 13:40
  • `separate(df, col = COL1, into = paste0("COL1_", 1:3))`? – David Arenburg Oct 15 '15 at 13:42
  • Thanks for the replies ! akrun: problem with this proposal is that this keeps the original column and does not transmute it. david: Problem is I still have to specify the `3` and the original column name `COL1_` by hand – Boern Oct 15 '15 at 13:45

1 Answers1

1

This can be automated with cSplit

library(splitstackshape)
 cSplit(df, 'COL1', ' ')
akrun
  • 874,273
  • 37
  • 540
  • 662