Reputation: 1013
I want to update several columns in a big table with household survey data. About 20 columns have expenditures reported as negative values. However, I want to have the absolute values (for producing a latex table). I only manage to produce a new data.table with only these updated columns. Here is an example. I only want to have to update the columns 2 and 3:
library(data.table)
test <- data.table(c(1,2,3,4),c(-2,-3,-4,-5),c(-1,-4,-5,-6),c(1,2,3,6))
test[,lapply(.SD,abs),.SDcols=2:3]
This gives me a data.table with the columns 2 and 3 and not the full data.table.
I can easily do this making data.frames and using cbind:
df1.test<-data.frame(test)[,-c(2:3)]
df2.test<-data.frame(test[,lapply(.SD,abs),.SDcols=2:3])
test<-data.table(cbind(df1.test,df2.test))
but perhaps there is a smarter way in data.table.
Thanks Renger
Upvotes: 2
Views: 924
Reputation: 886938
You could try
test[,2:3 := lapply(.SD,abs),.SDcols=2:3][]
Or a faster approach would be to use set
(as suggested by @Frank) as it provides direct assignment by reference with low overhead
for(j in 2:3){
set(test, i=NULL, j=j, value=abs(test[[j]]))
}
Upvotes: 4