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
library(dplyr); test <- test %>% mutate(V2 = abs(V2), V3=abs(V3)):=operator?