15
dt <- data.table(L=1:5,A=letters[7:11],B=letters[12:16])
   L A B
1: 1 g l
2: 2 h m
3: 3 i n
4: 4 j o
5: 5 k p

Now I want to paste columns "A" and "B" to get a new one, let's call it "new":

dt2
   L A B new
1: 1 g l  gl
2: 2 h m  hm
3: 3 i n  in
4: 4 j o  jo
5: 5 k p  kp
beginneR
  • 3,207
  • 5
  • 30
  • 52
  • If you use a data frame you can just use paste: `dt <- data.frame(L=1:5,A=letters[7:11],B=letters[12:16])` `dt$new <- paste0(dt$A,dt$B)` – dayne Aug 21 '13 at 14:03
  • ok I just wanted to ask you to post it. I can't accept my own answer without waiting for 2 days... – beginneR Aug 21 '13 at 14:36

4 Answers4

24

I had a similar issue but had many columns, and didn't want to type them each manually.

New version

(based on comment from @mnel)

dt[, new:=do.call(paste0,.SD), .SDcols=-1]

This is roughly twice as fast as the old version, and seems to sidestep the quirks. Note the use of .SDcols to identify the columns to use in paste0. The -1 uses all columns but the first, since the OP wanted to paste columns A and B but not L.

If you would like to use a different separator:

dt[ , new := do.call(paste, c(.SD, sep = ":"))]

Old version

You can use .SD and by to handle multiple columns:

dt[,new:=paste0(.SD,collapse=""),by=seq_along(L)]

I added seq_along in case L was not unique. (You can check this using dt<-data.table(L=c(1:4,4),A=letters[7:11],B=letters[12:16])).

Also, in my actual instance for some reason I had to use t(.SD) in the paste0 part. There may be other similar quirks.

dayne
  • 7,504
  • 6
  • 38
  • 56
dnlbrky
  • 9,396
  • 2
  • 51
  • 64
17

Arun's comment answered this question:

dt[,new:=paste0(A,B)]
Community
  • 1
  • 1
beginneR
  • 3,207
  • 5
  • 30
  • 52
  • 3
    This is the precise method I used for a very similar task, however I decided to use the option `collapse = "."` as I am using the resulting column, `new`, as an ID of sorts, and the addition of the `collapse =` option changes the behaviour unexpectedly -- on a decently sized `data.table` it effectively froze R -- is this actually expected behaviour or a bug? ie: `dt[, new := paste0(A, B, collapse = "."]` which yields the following on the sample table used in this example: `l g l l.h m.i n.j o.k p` – daRknight Mar 27 '17 at 17:51
  • What to do if I want to name the column "new" as "AB" ie I want to append the column names as well – Abhishek Singh Jul 04 '17 at 07:38
  • Just use names(dt)[names(dt)=="new"]<-"AB" – Max M Nov 09 '17 at 10:31
-1

This should do it:

 dt <- data.table(dt, new = paste(dt$A, dt$B, sep = ""))
Mayou
  • 8,498
  • 16
  • 59
  • 98
-3

If you want to paste strictly using column indexes (when you may not know the row names)... I want to get a new column by pasting two columns 6 and 4

dt$new <- apply( dt[,c(6,4)], 1, function(row){ paste(row[1],row[2],sep="/") })
Sqripter
  • 101
  • 2
  • 7