I'm used to using the SE versions of dplyr
verbs in for loops and am wanting to transition to the new evaluation semantics, but I'm struggling.
In older dplyr versions I would do something like:
df <- tribble(
~x, ~y, ~z,
"a", 2, "dog",
"b", 1, "cat",
"a", 2, "cat"
)
for (i in names(df %>% select(x,z))){
print(count_(df,i))
}
# A tibble: 2 x 2
x n
<chr> <int>
1 a 2
2 b 1
# A tibble: 2 x 2
z n
<chr> <int>
1 cat 2
2 dog 1
I have tried various combinations of quo
/enquo
/!!
/!!!
and can't seem to make it work using count()
.
Use sym
to turn the string into a symbol, then use !!
to insert the symbol into the expression
for (i in names(df %>% select(x,z))){
print(count(df, !!sym(i)))
}