Is there a quick 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 looks for variable names and labels). Therefore, lookfor education quickly finds variables related to education. Is there an equivalent quick access function in R?

+6
source share
3 answers

You can just grep data.frame for the necessary information. Then you will get much more information than just a list of variable names for which someone is mapped. You can also use regular expressions, thereby increasing your search capabilities. Here is an example of a function that does what you want (works only with data.frame):

 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 only save information about whether grep matches anything and writes it for each column separately. Instead ... you can pass any arguments to grep . If you omit it, this function will perform 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" 
+6
source

How about this as an oneliner, which I run at the beginning of the session:

 lkf <- function(d,p) names(d)[grep(p,names(d))] 

where d is the name of your data.frame and p is the template.

So,

 d <- data.frame(a=letters[1:10],b=1:10,c=month.name[1:10]) lkf(d,'c') # [1] "c" 

And here is a version that does not require you to specify variable names

 lookfor <- function(string_to_find, data){ # Extract the arguments and force conversion to string pars <- as.list(match.call()[-1]) data.name <- as.character(pars$data) var <- as.character(pars$string_to_find) # Regular expression search through names result <- names(data)[grep(var, names(data))] if(length(result) == 0) { warning(paste(var, "not found in", data.name)) return(NULL) } else { return(result) } } 
+2
source

If you just need to find a list of variables to find the one you are looking for, then you can use the code completion function in RStudio (v0.99 onwards). Just start typing and you will get a list of possible matches. Therefore, in your case, enter education$ and a list of variables contained in the data frame will appear. Select them and select the one you want.

0
source

Source: https://habr.com/ru/post/927184/


All Articles