Using dplyr filter() in programming
I agree with @hrbrmstr's standard evaluation solution. As suggested by @hadley today here's NSE solution:
library(tidyverse)
df <-data.frame(x = sample(1:100, 50),
y = rnorm(50),
z = sample(1:100,50),
w = sample(1:100, 50),
p = sample(1:100,50))
new <- function(ang, brad, drau){
ang <- enquo(ang)
brad <- enquo(brad)
drau <- enquo(drau)
df %>% filter(UQ(drau) %in% 1:50) %>%
select(UQ(ang),UQ(brad))
}
brand <- c("z","w","p")
brand <- rlang::syms(brand)
map_df(brand, ~new(ang = x, brad = y, drau = UQ(.x)))
This appears to do what you want (but it needs confirmation by you):
library(tidyverse)
library(rlang)
set.seed(1492)
xdf <- data_frame(
x = sample(1:100, 50),
y = rnorm(50),
z = sample(1:100,50),
w = sample(1:100, 50),
p = sample(1:100,50)
)
new_df <- function(ang, brad, drau) {
drau <- sym(drau)
filter(xdf, UQE(drau) %in% 1:50) %>%
select(ang, brad)
}
brand <- c("z", "w", "p")
map_df(brand, ~new_df(ang = "x", brad = "y", drau = .x))
Despite there being a plethora of "official" "tidyverse" examples using df
, it's a function in the stats
pkg and I try to avoid using it anymore.
Since you're using the tidyverse, might as well take advantage of map_df()
from purrr
.