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"
source share