2

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

2
  • You can use the dplyr package library(dplyr); test <- test %>% mutate(V2 = abs(V2), V3=abs(V3)) Commented Mar 3, 2015 at 15:36
  • 1
    Are you familiar with the := operator? Commented Mar 3, 2015 at 15:38

1 Answer 1

4

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]]))
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.