0

I wanna merge the following columns

   p    t
   1    2
   3    4
   2    5

output:

  m
  1,2
  3,4
  2,5

2 Answers2

1

We can use paste

with(df1, paste(p, t, sep=","))

Or with unite

library(dplyr)
unite(df1, m, p, t, sep=",")
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Akrun's solution is excellent but I also like to do it with data.table if you're are using this package.

library(data.table)
dt <- as.data.table(dt)
dt <- dt[, m := paste0(p, ",", t)][,c(3)]

Output :

     m
1: 1,2
2: 3,4
3: 2,5
Gainz
  • 1,721
  • 9
  • 24