Skip to content

Instantly share code, notes, and snippets.

@mrdwab
Created May 12, 2015 17:01
Show Gist options
  • Select an option

  • Save mrdwab/640ec81bbfe183991185 to your computer and use it in GitHub Desktop.

Select an option

Save mrdwab/640ec81bbfe183991185 to your computer and use it in GitHub Desktop.
Used to select columns in a data.table according to the column names.
contains <- function(pattern) {
pattern <- pattern
call_info <- deparse(sys.calls()[[1]])
if (grepl("(^.+\\()(.+)(\\)$)",call_info)) {
this_name <- sub("(^.+\\()(.+)(\\)$)","\\2",call_info)
} else {
this_name <- strsplit(call_info,"\\[")[[1]][1]
}
this <- get(this_name)
this_names <- names(this)
eval.parent(grep(pattern, this_names))
}
startswith <- function(pattern) {
contains(paste0("^", pattern))
}
endswith <- function(pattern) {
contains(paste0(pattern, "$"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment