小编exh*_*r10的帖子

R:如何根据多个标准求和并总结表格

这是我的原始数据框:

df <- read.table(text="
  Date         Index  Event
  2014-03-31   A      x
  2014-03-31   A      x
  2014-03-31   A      y
  2014-04-01   A      y
  2014-04-01   A      x
  2014-04-01   B      x
  2014-04-02   B      x
  2014-04-03   A      x
  2014-09-30   B      x", header = T, stringsAsFactors = F)

date_range <- seq(as.Date(min(df$Date)), as.Date(max(df$Date)), 'days')
indices <- unique(df$Index)
events_table <- unique(df$Event)
Run Code Online (Sandbox Code Playgroud)

我想我需要的输出来概括我的数据帧,并有在每个指标的唯一记录索引和每个日期DATE_RANGE而在events_table在新的一列提供每个事件的累计值之前,在日期列中的值对所有日期.有时每个索引或每个日期都没有记录.

这是我想要的输出:

Date        Index  cumsum(Event = x) cumsum(Event = y)
2014-03-31  A      0                 0
2014-03-31  B      0                 0
2014-04-01  A      2                 1
2014-04-01 …
Run Code Online (Sandbox Code Playgroud)

r plyr dplyr

7
推荐指数
1
解决办法
1897
查看次数

标签 统计

dplyr ×1

plyr ×1

r ×1