在dplyr的Summary函数中使用filter函数
是否有办法在摘要中使用过滤功能,例如:在dplyr的Summary函数中使用filter函数,r,dplyr,R,Dplyr,是否有办法在摘要中使用过滤功能,例如: #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
#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))
您可以使用:
library(dplyr)
data %>%
summarise(Price = mean(Price[Type != 'SELL']),
Amount = sum(Amount),
USD = sum(USD),
date = min(date))
要在管道中使用它,请执行以下操作:
data %>%
summarise(Price = mean(data %>% filter(Type != 'SELL') %>% pull(Price)),
Amount = sum(Amount),
USD = sum(USD),
date = min(date))
谢谢,为了确保不可能在summary函数中使用管道?是的,但是我发现这项任务非常复杂。请参阅更新的答案。