juwi
juwi

Reputation: 98

passing column name as variable in dplyr

Variants of this question have been asked a lot, I also read about NSE. Still I cannot figure this out.

This is easy:

library(dplyr)
data(cars)

cars %>%
  group_by(speed) %>%
  summarise(d = mean(dist))

Now I want to use variable x to pass the dist column to mean

x <- "dist"

Of course this does not work:

cars %>%
  group_by(speed) %>%
  summarise(d = mean(x))

So I use SE version of summarise:

cars %>%
  group_by(speed) %>%
  summarise_(d = mean(x))

Ok, does not work, so I have to add ~ as well:

cars %>%
  group_by(speed) %>%
  summarise_(d = ~mean(x))

Still does not work, but if use dist instead of x:

cars %>%
  group_by(speed) %>%
  summarise_(d = ~mean(dist))

This works, but doesn't use x.

cars %>%
  group_by(speed) %>%
  summarise_(d = ~mean(~x))

This also doesn't work.

I'm basically monkeying around without any idea how to make this work, or why it fails.

Upvotes: 0

Views: 424

Answers (1)

Adam Quek
Adam Quek

Reputation: 7153

cars %>%
    group_by(speed) %>%
    summarise_each_(funs(mean), vars(matches(x)))

Upvotes: 2

Related Questions