Search code examples
rstata

Is there a fast way to search for variables in R?


In Stata the lookfor command offers a quick way to search for variables in a dataset (and it searches both the variable names and labels). So lookfor education quickly finds you variables related to education. Is there an equivalent shortcut function in R?


Solution

  • You can simply grep the data.frame for the information necessary. Then you'll get much more information than simply the list of names of variables for which somebody is matched. You can also use regular expressions, thus enhancing your search capabilities. Here is the example of a function which does what you want (works with data.frame only):

    lookfor <- 
    function (pattern, data, ...) 
    {
        l <- lapply(data, function(x, ...) grep(pattern, x, ...))
        res <- rep(FALSE, ncol(data))
        res[grep(pattern, names(data), ...)] <- TRUE
        res <- sapply(l, length) > 0 | res
        names(res) <- names(data)
        names(res)[res]
    }
    

    First I grep each column, then I grep the column names. Then I keep only information whether grep matched anything and record it for each column separately. Instead of ... you can pass any arguments to grep. If you omit it, this function will do a simple string matching.

    Here is an example:

    > dt<- data.frame(y=1:10,x=letters[1:10],a=rnorm(10))
    > lookfor("a",dt) 
    [1] "x" "a"