【问题标题】:Calculate percentage changes in a data frame for subgroups over various periods of time计算不同时间段内子组的数据框中的百分比变化
【发布时间】:2017-12-18 22:52:51
【问题描述】:

有一些类似的问题,但是我没有遇到对我的特殊情况有帮助的问题;每个季度都会为每个事件名称记录一个事件计数,为每个事件完成几次传递以捕获丢失的计数。我想将每次传递中计数的百分比变化与他们之前在各自传递中的计数进行比较。

这是我目前拥有的数据(值不同但格式相同):

ID <- c(221, 221, 345, 345, 209, 209, 209, 19, 19, 19, 536, 536, 536)
Pass <- c(1, 2, 1, 2, 1, 2, 3, 1, 2, 3, 1, 2, 3)
Event_count <- c(2000, 100, 2050, 150, 50000, 10000, 600, 51000, 11000, 700, 50500, 10500, 650)
Event_name <- c(rep('filter', 4) , rep('observations', 9))
Date <- c(rep('2015-03-01',2) , rep('2015-06-01',2) , rep('2015-03-01',3) , rep('2015-06-01',3), rep('2015-09-01',3))  
df <- data.frame(ID, Pass, Event_count, Event_name, Date)

    ID Pass Event_count   Event_name       Date
1  221    1        2000       filter 2015-03-01
2  221    2         100       filter 2015-03-01
3  345    1        2050       filter 2015-06-01
4  345    2         150       filter 2015-06-01
5  209    1       50000 observations 2015-03-01
6  209    2       10000 observations 2015-03-01
7  209    3         600 observations 2015-03-01
8   19    1       51000 observations 2015-06-01
9   19    2       11000 observations 2015-06-01
10  19    3         700 observations 2015-06-01
11 536    1       50500 observations 2015-09-01
12 536    2       10500 observations 2015-09-01
13 536    3         650 observations 2015-09-01

这就是我想要的输出(如果你能想到更好的方法来显示这些数据,请告诉我!)

Percentage_change <- c(NA, NA, 2.5, 50, NA, NA, NA, 2, 10, 16.67, -0.98, -4.55, -7.14)
df2 <- data.frame(ID, Pass, Event_count, Event_name, Date,Percentage_change)

    ID Pass Event_count   Event_name       Date Percentage_change
1  221    1        2000       filter 2015-03-01                NA
2  221    2         100       filter 2015-03-01                NA
3  345    1        2050       filter 2015-06-01              2.50
4  345    2         150       filter 2015-06-01             50.00
5  209    1       50000 observations 2015-03-01                NA
6  209    2       10000 observations 2015-03-01                NA
7  209    3         600 observations 2015-03-01                NA
8   19    1       51000 observations 2015-06-01              2.00
9   19    2       11000 observations 2015-06-01             10.00
10  19    3         700 observations 2015-06-01             16.67
11 536    1       50500 observations 2015-09-01             -0.98
12 536    2       10500 observations 2015-09-01             -4.55
13 536    3         650 observations 2015-09-01             -7.14

我只有相对基本的 R 知识,所以我不知道是否有任何软件包可以帮助我解决这个问题 - 您可以提供给我的任何帮助/解释将不胜感激。

【问题讨论】:

    标签: r dataframe time-series percentage


    【解决方案1】:

    这似乎返回了你想要的值

    library(dplyr)
    df %>% 
      group_by(Event_name, Pass) %>% 
      mutate(Percentage_change=(Event_count/lag(Event_count)-1)*100)
    

    【讨论】:

    • 这么简洁的解决方案,谢谢!这种方法还将使绝对计数的变化变得超级容易,这就是我接下来要做的。
    猜你喜欢
    • 2022-01-26
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多