Suppose I have data frame in R
A B
d test1
e test2
Suppose I want to combine two columns A B to a new column C which is list of columns A and B
A B C
d test1 (d,test1)
e test2 (e,test2)
Suppose I have data frame in R
A B
d test1
e test2
Suppose I want to combine two columns A B to a new column C which is list of columns A and B
A B C
d test1 (d,test1)
e test2 (e,test2)
Data:
df <- data.frame(
A = c("d","e"),
B = c("test1", "test2"), stringsAsFactors = F)
Solution:
apply the function paste0 to rows (1) in df, collapsing them by ,and assign the result to df$C:
df$C <- apply(df,1, paste0, collapse = ",")
Result:
df
A B C
1 d test1 d,test1
2 e test2 e,test2