[datatable-help] What's the trick to paste several columns together in one

Frank Erickson fperickson at wisc.edu
Thu Jul 23 21:19:18 CEST 2015


I think you want

DT[,(newName):=do.call(paste,c(list(sep="_"),.SD)),.SDcols=colNames]

This is probably the sort of thing that would come naturally after studying
all the vignettes and such on the site:
https://github.com/Rdatatable/data.table/wiki/Getting-started Dunno that
you want to be sending so much stuff to everyone on a mailing list. Your
call, though.

--Frank

On Thu, Jul 23, 2015 at 3:13 PM, statquant3 <statquant at outlook.com> wrote:

> R) data.table(x=letters[1:5],y=rnorm(5),z=LETTERS[1:5])
>     x                     y  z
> 1: a  0.5551301186 A
> 2: b  0.1092535710 B
> 3: c -2.4319115661 C
> 4: d  1.6915588561 D
> 5: e -1.5163715976 E
>
> colNames = c('y','z')
> newName = 't'
>
> How do I add a column 't' such that t:=paste(y,x,sep='_')
>
>     x                     y  z t
> 1: a  0.5551301186 A 0.5551301186_A
> 2: b  0.1092535710 B 0.1092535710_B
> 3: c -2.4319115661 C ...
> 4: d  1.6915588561 D ...
> 5: e -1.5163715976 E ...
>
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/What-s-the-trick-to-paste-several-columns-together-in-one-tp4710266.html
> Sent from the datatable-help mailing list archive at Nabble.com.
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20150723/43f9aeb8/attachment.html>


More information about the datatable-help mailing list