逆向工程累积到每日数据?

Reverse engineering cumulative to daily data?

我有一个数据框,其中包含日期数据和累计计数。 我正在尝试对 cumsum 进行 反转以获取每日计数 还获取每组的计数。 我正在尝试从数据框 A 转到数据框 B。 我正在使用 R 和 tidyr.

这是代码:


df <- data.frame(cum_count = c(5, 14, 50, 5, 14, 50),
                 state = c("Alabama", "Alabama", "Alabama", "NY", "NY", "NY"),
                 Year = c(2012:2014, 2012:2014))

Dataframe A
  cum_count   state Year
1         5 Alabama 2012
2        14 Alabama 2013
3        50 Alabama 2014
4         5      NY 2012
5        14      NY 2013
6        50      NY 2014
Dataframe B
  cum_count   state Year
1         5 Alabama 2012
2         9 Alabama 2013
3        36 Alabama 2014
4         5      NY 2012
5         9      NY 2013
6        36      NY 2014

我试过使用 diff 函数:

df <- df %>%group_by(state)%>%
      mutate(daily_count = diff(cum_count))

但是我明白了

Error: Column daily_count must be length 3 (the number of rows) or one, not 2

告诉我你的想法。

谢谢!

也许你可以试试diff,例如

df <- df %>%group_by(state)%>%
  mutate(daily_count = c(cum_count[1],diff(cum_count)))

这样

> df
# A tibble: 6 x 4
# Groups:   state [2]
  cum_count state    Year daily_count
      <dbl> <chr>   <int>       <dbl>
1         5 Alabama  2012           5
2        14 Alabama  2013           9
3        50 Alabama  2014          36
4         5 NY       2012           5
5        14 NY       2013           9
6        50 NY       2014          36

这是一个基本的 R 选项,来自 ave

df <- within(df,daily_count <- ave(cum_count,state,FUN = function(x) c(x[1],diff(x))))

这样

> df
  cum_count   state Year daily_count
1         5 Alabama 2012           5
2        14 Alabama 2013           9
3        50 Alabama 2014          36
4         5      NY 2012           5
5        14      NY 2013           9
6        50      NY 2014          36