To select a couple columns from a dataframe I can do
require(dplyr)
require(magrittr)
df <- data.frame(col1=c(1, 2, 3), col2=letters[1:3], col3=LETTERS[4:6])
df %>%
select(col1, col2)
I want to write a function similar to
f <- function(data, firstCol, secondCol){
data %>%
select(substitute(firstCol), substitute(secondCol))
}
But running f(df, col1, col2)
gives me the error
Error in select_vars(names(.data), ..., env = parent.frame()) :
(list) object cannot be coerced to type 'double'
Called from: (function ()
{
.rs.breakOnError(TRUE)
})()
EDIT -- slightly less trivial example:
Suppose I wanted to do
mtcars %>%
select(cyl, hp) %>%
unique %>%
group_by(cyl) %>%
summarise(avgHP = mean(hp))
but with different datasets and different variable names. I could reuse the code and replace mtcars
, cyl
, and hp
. But I'd rather wrap it all up in a function
It's pretty simple in this case, since you can just use ...
f <- function(data, ...) {
data %>% select(...)
}
f(df, col1, col2)
#> col1 col2
#> 1 1 a
#> 2 2 b
#> 3 3 c
In the more general case, you have two options:
substitute()
and then
eval()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With