[datatable-help] Update data.table columns by multiplication of another column

Arunkumar Srinivasan aragorn168b at gmail.com
Sun Oct 27 10:58:33 CET 2013


I realise that if you may be using 1.8.10 (and not dev. version 1.8.11), you may have to provide column indices to "j" instead of column names. So this should work:

for(j in which(names(dt) %chin% v)) set(dt, i=NULL, j=j, dt[[j]]*dt[[coef]]) 

Arun


On Sunday, October 27, 2013 at 10:54 AM, Arunkumar Srinivasan wrote:

> How about this?
> for (j in v) set(dt, i=NULL, j=j, dt[[j]]*dt[[coeff]]) 
> 
> Arun
> 
> 
> On Sunday, October 27, 2013 at 9:37 AM, cduprez wrote:
> 
> > Hi all,
> > 
> > I am trying to update columns of a data.table, whose names are in a vector,
> > by multiplicating their values with the values of another column (whose name
> > is in another vector).
> > Example : 
> > dt <- data.table(a=c(1, 1, 1, 1, 1), b=c(2, 2, 2, 2, 2), c=c(3, 3, 3, 3, 3),
> > d=c(4, 4, 4, 4, 4), e=c(5, 5, 5, 5, 5), coef = c(1, 2, 3, 4, 5))
> > v <- c("b", "c")
> > coef <- c("coef")
> > 
> > dt
> > a b c d e coef
> > 1: 1 2 3 4 5 1
> > 2: 1 2 3 4 5 2
> > 3: 1 2 3 4 5 3
> > 4: 1 2 3 4 5 4
> > 5: 1 2 3 4 5 5
> > 
> > And what I am looking for, as result, is : b = b*coef, c = c*coef
> > a b c d e coef
> > 1: 1 2 3 4 5 1
> > 2: 1 4 6 4 5 2
> > 3: 1 6 9 4 5 3
> > 4: 1 8 12 4 5 4
> > 5: 1 10 15 4 5 5
> > 
> > How can I compute that result by keeping the columns to update and the coef
> > column in character vectors containing the columns names.
> > I precise that the coef vector still contains only one column name.
> > 
> > Thanks in advance for you help.
> > 
> > Regards,
> > 
> > Cedric
> > 
> > 
> > 
> > --
> > View this message in context: http://r.789695.n4.nabble.com/Update-data-table-columns-by-multiplication-of-another-column-tp4679124.html
> > Sent from the datatable-help mailing list archive at Nabble.com (http://Nabble.com).
> > _______________________________________________
> > datatable-help mailing list
> > datatable-help at lists.r-forge.r-project.org (mailto: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/20131027/910e802e/attachment.html>


More information about the datatable-help mailing list