0
votes

Is there a way to use the filtering function inside the summarise for example:

#I have a dataset with 5 columns Price, Type, Amount USD, date 
# I only want the mean price of the rows that are not Type == "SELL"

data = summarise(Price = filter(!Type == "SELL") %>% mean(Price), Amount = sum(Amount), USD = sum(USD), date = min(date))
1

1 Answers

1
votes

You can use :

library(dplyr)

data %>%
  summarise(Price = mean(Price[Type != 'SELL']), 
            Amount = sum(Amount), 
            USD = sum(USD), 
            date = min(date))

To use it in pipes :

data %>%
  summarise(Price = mean(data %>% filter(Type != 'SELL') %>% pull(Price)), 
            Amount = sum(Amount), 
            USD = sum(USD), 
            date = min(date))