R DataFrame:如何将某个值的出现次数传递到由另一个变量分组的新数据框中

R Data Frame: How to pass number of occurences of a certain value into a new dataframe grouped by another variable

我有一个包含不同 activites/events、它们发生的日期和 activity 的持续时间的数据框。我现在想创建一个新的数据框,其中包含仅按天分组的事件 B 的发生次数(因此两列,日期和发生次数)。 Daily_Duration 是我想要的其他数据框格式的按天计算的所有事件的持续时间示例。

library(dplyr)

df <- data.frame(
  Event = c ("A", "B", "C", "B", "C", "B", "B"), 
  Day = c("Day 1", "Day 1", "Day 1", "Day 2", "Day 2","Day 2","Day 2")
  Duration = c(1,2,4,5,1,3,5))

Daily_Duration<- aggregate(Daten$Duration, list(Daten$Day), FUN=sum) 


我试过了


Event_B_by_day<- df[df$Event == 'B', ]%>%
  group_by(df$Day) %>%
  summarise(Freq = length(df$Event))

这给了我以下错误:

Error: Problem adding computed columns in `group_by()`.
x Problem with `mutate()` input `..1`.
i `..1 = df$Day`.
i `..1` must be size 4 or 1, not 7.

Event_B_by_day<- aggregate(df[df$Event=="B"], list(df$Day), FUN=length ) 
 

其中 returns 一个未被事件 B 过滤的数据帧,但按天将所有三个事件的发生次数相加,因此等同于:

Event_B_by_day<- aggregate(df$Event, list(df$Day), FUN=length ) 

那么错误在哪里,我如何真正获得我想要的数据框?

基础 R

使用公式。

aggregate(Duration ~ Day, data = df[df$Event == "B",], FUN = sum)

dplyr

df %>% 
  filter(Event == "B") %>% 
  group_by(Day) %>% 
  summarise(Duration = sum(Duration))

输出

  Day   Duration
1 Day 1        2
2 Day 2       13