Back
I have the following data frame:
> dfX1 X2 X3 1 3 4 1 0 0 1 1 0
> df
X1 X2 X3
1 3 4
1 0 0
1 1 0
and I want to merge all the column so that the final output will be:
new colName1 X11 X11 X13 X20 X21 X24 X30 X30 X3
new colName
1 X1
3 X2
0 X2
1 X2
4 X3
0 X3
You can try the stack:
> setNames(stack(df),c("new","colName")) new colName1 1 X12 1 X13 1 X14 3 X25 0 X26 1 X27 4 X38 0 X39 0 X3
> setNames(stack(df),c("new","colName"))
1 1 X1
2 1 X1
3 1 X1
4 3 X2
5 0 X2
6 1 X2
7 4 X3
8 0 X3
9 0 X3
Data
> dput(df)structure(list(X1 = c(1L, 1L, 1L), X2 = c(3L, 0L, 1L), X3 = c(4L, 0L, 0L)), class = "data.frame", row.names = c(NA, -3L))
> dput(df)
structure(list(X1 = c(1L, 1L, 1L), X2 = c(3L, 0L, 1L), X3 = c(4L,
0L, 0L)), class = "data.frame", row.names = c(NA, -3L))
If you are a beginner and want to know more about R then do refer to the R programming tutorial.
31k questions
32.8k answers
501 comments
693 users