-1

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)
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
teotjunk
  • 11
  • 1

1 Answers1

0

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
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34