Last active
August 29, 2015 14:10
-
-
Save datalove/cdf5cb65a72cf901dd45 to your computer and use it in GitHub Desktop.
Elegantly pull a column vector from a dplyr table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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