Skip to content

Instantly share code, notes, and snippets.

@datalove
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save datalove/cdf5cb65a72cf901dd45 to your computer and use it in GitHub Desktop.

Select an option

Save datalove/cdf5cb65a72cf901dd45 to your computer and use it in GitHub Desktop.
Elegantly pull a column vector from a dplyr table
library(dplyr)
# you can pull out a column from a tbl_df like this, but it's ugly and awkward to type
mtcars %>% .[['mpg']]
mtcars %>% .[[1]]
# So let's make a nice function
pull <- function(x,y) {x[,if(is.name(substitute(y))) deparse(substitute(y)) else y]}
# works on tbl_df or dataframes equally well
mtcars %>% pull('mpg')
mtcars %>% pull(mpg)
mtcars %>% pull(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment