[datatable-help] How to replace values in data.table conditionally

Manabu Sakamoto manabu.sakamoto at gmail.com
Thu Mar 13 15:48:18 CET 2014


Dear list

I'm trying to access values within a data.table column by matching to
elements in a vector and replacing with corresponding elements in a second
vector. But I want to loop through specific column names also stored as a
character vector.
So something like:

DT <- data.table(A=seq(1:10),B=seq(1:10),C=seq(1:10))

cnm <- c("A", "B", "C")
before <- c(4, 5, 6)
after <- c(3, 7, 8)

nm <- cnm[i]
bfr <- before[i]
afr <- after[i]

DT[nm==bfr, nm:=afr]

I'm sure this is completely wrong because it didn't work.
So does anyone know how to correctly do this data.table solution?

Many thanks,
Manabu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20140313/ff164115/attachment.html>


More information about the datatable-help mailing list