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

cduprez cedric.duprez at ign.fr
Sun Oct 27 09:37:33 CET 2013


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.


More information about the datatable-help mailing list