8

For Example the following will not work:

data(mpg)

filter(mpg, manufacturer =="audi") %>%
    sum(.$cty)

However the following will work:

data(mpg)

x <- filter(mpg, manufacturer =="audi")

sum(x$cty)

I would like to get it all to work in a piping flow if possible.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Angel Cloudwalker
  • 2,015
  • 5
  • 32
  • 54

2 Answers2

12

You could use pull to get column as a vector and then use sum.

library(dplyr)

mpg %>%
  filter(manufacturer =="audi") %>%
  pull(cty) %>%
  sum
#[1] 317

Your attempt does not work because pipes send output of LHS as first argument to the function in RHS. So a dataframe is being passed as first argument in sum. It will work if you use {} around it.

filter(mpg, manufacturer =="audi") %>% {sum(.$cty)}
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • what is LHS and RHS, and what do the {} do essentially in laymen terms? – Angel Cloudwalker Feb 28 '20 at 00:29
  • 2
    @MilesMorales Left-hand-side, right-hand-side. If you do `mpg %>% summarise(sum = sum(cty))` the first argument of `?summarise` is `.data` which is `mpg` and it is implicitly passed to `summarise` here using pipes. Using `{}` we can stop that behavior. – Ronak Shah Feb 28 '20 at 00:32
0

We could do this without the filter step by doing this in summarise itself

library(dplyr)
library(ggplot2)
mpg %>% 
   summarise(out = sum(cty[manufacturer == "audi"])) %>%
   pull(out)
 #[1] 317
akrun
  • 874,273
  • 37
  • 540
  • 662