Lets say I have a data table like the one below:
library(data.table) N = 10 x = data.table(id = 1:N, segm = sample(c("A","B","C"),N,replace=T), r = rnorm(N,20,5), aa = sample(0:1,N,replace=T), ab = sample(0:1,N,replace=T), ba = sample(0:1,N,replace=T), bb = sample(0:1,N,replace=T))
I'd like to know how to substitute the 1 values for
NA but only for the columns
bb using the data table package. I know how to do this using data frame.
I tried using the following:
f = c("aa","ab","ba","bb") x[,f,with=F][x[,f,with=F]==1] <- "NA"
but I'm getting an error:
Error in [<-.data.table(*tmp*, , f, with = F, value = list(aa = c("0", : unused argument (with = F)
To sum up, my question is: How can I subset and assign on several columns of a data table at the same time.
The line of code:
is just not working. Why?
Any help is appreciate.