Reputation: 1389
I must be missing something reading the tidyverse programming vignette. For this example:
library(dplyr)
library(entropy)
set.seed(122)
df <-data.frame(vec = sample(letters[1:20],100,replace = T),val = sample(1:10,100,replace = T))
df %>%
group_by(vec) %>%
filter(
n()>4,
entropy(table(val))>0
)
# A tibble: 62 x 2
# Groups: vec [8]
vec val
<fct> <int>
1 s 10
2 s 8
3 d 4
4 a 2
5 l 4
6 m 4
7 a 5
8 a 9
9 l 4
10 s 6
# ... with 52 more rows
I would like to pass the value variable as a string but this doesn't work here:
var_name="val"
df %>%
group_by(vec) %>%
filter(
n()>4,
entropy(table(!! var_name))>0
)
# A tibble: 0 x 2
# Groups: vec [1]
# ... with 2 variables: vec <fct>, val <int>
What am I missing?
Upvotes: 0
Views: 58
Reputation: 269491
Use !!sym(var_name)
like this:
df %>%
group_by(vec) %>%
filter(
n()>4,
entropy(table(!!sym(var_name)))>0
)
Upvotes: 1