1

When working with data imported from SPSS I need to access value.labels attribute of some variables.

The names of these variables I get as strings. The only way to pass the variable name (as string) to attr which I have found is through get() but it is very very slow. The situation is quite simple so there is probably a better way how to do it.

The example is artificial but the size (number of variables) is similar to what I work with in real. I also need the data.table in the real situation.

library(data.table)

id <- c(1:4000) 
x <- sample(1:3, 4000, replace=T)
dv<-c(id,x)
df<-data.frame(t(dv))
dt<-data.table(df)

attr(dt$X1,"value.labels")<-c("first","second","third")


#direct approach is fast
pm<-proc.time()
attr(dt[,X1],"value.labels")
proc.time() - pm

   user  system elapsed 
   0.00    0.04    0.03 

#using variable name as a string is slow
pm<-proc.time()
attr(dt[,get("X1")],"value.labels")
proc.time() - pm

   user  system elapsed 
   0.85    0.03    0.89 

The get() solution is incredibly slow but it works.

attr(dt[,"X1", with=FALSE],"value.labels")
NULL

attr(dt[,match("X1",names(dt)),with=FALSE],"value.labels")
NULL

Does not work.

Martin
  • 165
  • 1
  • 9

0 Answers0