Looking for variables in R

Recently, I have been working with big databases. After reading their codebooks (usually very long pdf files), I thought it would be useful to have a function to find variable names in R. I wrote a simply function that looks for variable names in data.frame and data.table objects.

Here an example:

library(devtools); source_gist("4661324")
library(data.table)
dat  <- data.table(infert)
(var  <- lookvar(dat, c("par", "spon")))
## [1] "parity"      "spontaneous"
dat[, var, with=FALSE]
##      parity spontaneous
##   1:      6           2
##   2:      1           0
##   3:      6           0
##   4:      4           0
##   5:      3           1
##  ---                   
## 244:      1           1
## 245:      1           0
## 246:      2           0
## 247:      1           1
## 248:      1           1

Pretty useful, at least for me. You can also use regular expressions to get variables, for instance, something like lookvar(dat, "p5[0-2]_[a-z]+_2").




blog comments powered by Disqus