This generic function is primarily useful for functions that accept column names for
subsetting data. The goal is to provide methods for users to supply either an
unquoted column name or a quoted one (character string). It makes use of the
fact that calling substitute()
on a character string has a class
"character", and class "name" otherwise.
get_sub_name(x)
x | The substituted column name. |
---|
The character vector for use with subsetting data.
get_col <- function(data, column) { name <- substitute(column) data[, get_sub_name(name)] } get_col(mtcars, hp)#> [1] 110 110 93 110 175 105 245 62 95 123 123 180 180 180 205 215 230 66 52 #> [20] 65 97 150 150 245 175 66 91 113 264 175 335 109get_col(mtcars, "hp")#> [1] 110 110 93 110 175 105 245 62 95 123 123 180 180 180 205 215 230 66 52 #> [20] 65 97 150 150 245 175 66 91 113 264 175 335 109