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
.