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.
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)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With