Skip to content

Instantly share code, notes, and snippets.

@leeper
Last active August 29, 2015 14:00
Show Gist options
  • Save leeper/3f98bd103f29ec83ca8d to your computer and use it in GitHub Desktop.
Save leeper/3f98bd103f29ec83ca8d to your computer and use it in GitHub Desktop.
Search for names, labels, and levels in R
# This is an effort to emulate Stata's `lookfor` in R
lookfor <- function(what, ls_opts = list(), ...){
s <- do.call("ls", ls_opts)
d <- lapply(s, lookin, what = what, ...)
# return value should be a list with the string matching the search, along with details of its position
# big challenge is doing this recursively because, e.g., lists of lists of dataframes would be really difficult to search
class(d) <- 'lookfor'
return(d)
}
print.lookfor <- function(x, ...){
# something like:
cat('Item\t\tLocation\n')
for(i in seq_along(x))
cat(x[[i]]$object, '\t\t', x[[i]]$location,'\n')
return(invisible(x))
}
.in_values <- function(x, what, ignore.case = FALSE, ...) {
w <- which(grepl(what, x, ...))
structure(setNames(w, x[w]), location = "values")
}
.in_names <- function(x, what, ignore.case = FALSE, ...) {
w <- which(grepl(what, names(x), ...))
structure(setNames(w, names(x)[w]), location = "names")
}
.in_colnames <- function(x, what, ignore.case = FALSE, ...) {
w <- which(grepl(what, colnames(x), ...))
structure(setNames(w, colnames(x)[w]), location = "colnames")
}
.in_rownames <- function(x, what, ignore.case = FALSE, ...) {
w <- which(grepl(what, rownames(x), ...))
structure(setNames(w, rownames(x)[w]), location = "rownames")
}
.in_comment <- function(x, what, ignore.case = FALSE, ...) {
w <- which(grepl(what, comment(x), ...))
structure(setNames(w, comment(x)[w]), location = "comment")
}
.in_levels <- function(x, what, ignore.case = FALSE, ...) {
w <- which(grepl(what, levels(x), ...))
structure(setNames(w, levels(x)[w]), location = "levels")
}
.in_attributes <- function(x, what, ignore.case = FALSE, ...) {
a <- attributes(x)
a$class <- NULL
a$levels <- NULL
if(length(a))
structure(lookin(a, what, ...), location = "attributes")
else
structure(NULL, location = "attributes")
}
lookin <- function(x, what, ...) UseMethod("lookin")
lookin.default <- function(x, what, ...) {
}
lookin.character <- function(x, what, ...){
c(.in_values(x, what, ...),
.in_comment(x, what, ...),
#.in_attributes(x, what, ...),
.in_names(x, what, ...))
}
lookin.numeric <- function(x, what, ...){
c(.in_comment(x, what, ...),
#.in_attributes(x, what, ...),
.in_names(x, what, ...))
}
lookin.logical <- function(x, what, ...){
c(.in_values(x, what, ...),
.in_comment(x, what, ...),
#.in_attributes(x, what, ...),
.in_names(x, what, ...))
}
lookin.factor <- function(x, what, ...){
c(.in_levels(x, what, ...),
.in_comment(x, what, ...),
#.in_attributes(x, what, ...),
.in_names(x, what, ...))
}
lookin.data.frame <- function(x, what, ...){
if(class(x) != 'data.frame')
stop("Object must be a data.frame")
c(.in_names(names(x), what, ...),
#.in_attributes(x, what, ...),
.in_comment(x, what, ...),
sapply(x, lookin, ...))
}
lookin.list <- function(x, what, ...){
c(.in_names(names(x), what, ...),
#.in_attributes(x, what, ...),
.in_comment(x, what, ...),
sapply(x, lookin, ...))
}
lookin.matrix <- function(x, what, ...){
if(class(x) != 'matrix')
stop("Object must be a matrix")
c(.in_rownames(x, what, ...),
.in_colnames(x, what, ...),
#.in_attributes(x, what, ...),
.in_comment(x, what, ...),
.in_values(x, what, ...))
}
# examples
d <- data.frame(hi = 1:3, hello = 4:6, hell = 5:7, b = c('hello','hi','hell'))
lookin(d, 'he')
m <- as.matrix(d)
lookin(m, 'he')
lookfor("he")
@leeper
Copy link
Author

leeper commented Mar 10, 2015

Development has moved here: https://github.com/leeper/lookfor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment