[英]How to use cumsum to get the cumulative sum of a freq table - R
I have the following frequency table (code copy below).我有以下频率表(下面的代码副本)。 This frequency table shows the monday of a given week and the number of observations on that given week.
此频率表显示给定周的星期一和该给定周的观察次数。
print(helper_table)
Week n
1: 10/04/2021 42
2: 10/11/2021 18
3: 10/18/2021 40
4: 10/25/2021 33
5: 11/01/2021 29
6: 11/08/2021 27
7: 11/15/2021 43
8: 11/22/2021 41
9: 11/29/2021 17
10: 12/06/2021 27
11: 12/13/2021 27
12: 12/20/2021 26
13: 12/27/2021 13
14: 01/03/2022 10
15: 01/10/2022 15
16: 01/17/2022 13
17: 01/24/2022 15
18: 01/31/2022 20
19: 02/07/2022 30
20: 02/14/2022 14
21: 02/21/2022 20
22: 02/28/2022 7
But my goal is to have another column that shows the cumulative frequency.但我的目标是有另一列显示累积频率。 I'm trying this code but its just duplicating the n column.
我正在尝试这段代码,但它只是复制了 n 列。 At thought that maybe because the
n
column was a integer that was preventing it from working but I did convert it to numeric.我想可能是因为
n
列是一个 integer 阻止它工作,但我确实将它转换为数字。
helper_table$n <- as.numeric(helper_table$n)
helper_table %>%
group_by(Week) %>%
#arrange(desc(x1)) %>%
mutate(cumsum=cumsum(n))
And this resulted in the following output:这导致以下 output:
# A tibble: 22 x 3
# Groups: Week [22]
Week n cumsum
<chr> <dbl> <dbl>
1 10/04/2021 42 42
2 10/11/2021 18 18
3 10/18/2021 40 40
4 10/25/2021 33 33
5 11/01/2021 29 29
6 11/08/2021 27 27
7 11/15/2021 43 43
8 11/22/2021 41 41
9 11/29/2021 17 17
10 12/06/2021 27 27
# ... with 12 more rows
Data:数据:
data.table::data.table(
Week = c("10/04/2021","10/11/2021","10/18/2021",
"10/25/2021","11/01/2021","11/08/2021","11/15/2021",
"11/22/2021","11/29/2021","12/06/2021","12/13/2021","12/20/2021",
"12/27/2021","01/03/2022","01/10/2022","01/17/2022",
"01/24/2022","01/31/2022","02/07/2022","02/14/2022","02/21/2022",
"02/28/2022"),
n = c(42,18,40,33,29,27,43,41,17,27,27,
26,13,10,15,13,15,20,30,14,20,7)
)
Perhaps changing:也许改变:
mutate(cumsum=cumsum(n))
By经过
mutate(cumsum=cumsum(1:n))
As pointed out by Gregor Thomas and Waldi in the comments, removing the grouping variable achieves the required result:正如 Gregor Thomas 和 Waldi 在评论中指出的那样,删除分组变量可以达到所需的结果:
helper_table %>%
mutate(cumsum=cumsum(n))
# Week n cumsum
#1: 10/04/2021 42 42
#2: 10/11/2021 18 60
#3: 10/18/2021 40 100
#4: 10/25/2021 33 133
#5: 11/01/2021 29 162
#...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.