I had a similar problem, but I had many columns, and I did not want to enter them manually.
A new version
(based on comment from @mnel)
dt[, new:=do.call(paste0,.SD), .SDcols=-1]
This is about two times faster than the old version and seems to wrap up quirks. Note the use of .SDcols to identify the columns used in paste0 . -1 uses all columns except the first, as the OP wanted to insert columns A and B, but not L.
If you want to use a different delimiter:
dt[ , new := do.call(paste, c(.SD, sep = ":"))]
Old version
You can use .SD and by to process multiple columns:
dt[,new:=paste0(.SD,collapse=""),by=seq_along(L)]
I added seq_along in case L was not unique. (You can verify 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.
dnlbrky
source share