如何对分组表进行突变

时间:2020-03-29 08:23:44

标签: r dplyr mutate

我有这样的'abc'表,

> abc
country   date      count
A      2020-03-01     1
A      2020-03-02     2
A      2020-03-03     3
B      2020-03-01     2
B      2020-03-02     5
B      2020-03-03     10
C      2020-03-01     1
C      2020-03-02     3
C      2020-03-03     6

我想用'rate'突变'abc'表,意思是“ x天的计数/(x-1)天的计数”。

像这样

> abc
country   date      count  rate
A      2020-03-01     1     NA
A      2020-03-02     2     2
A      2020-03-03     3     1.5
B      2020-03-01     2     NA
B      2020-03-02     5     2.5
B      2020-03-03     10    2
C      2020-03-01     1     NA
C      2020-03-02     3     3
C      2020-03-03     6     2

我该怎么做?

等待您的解决方案:)

2 个答案:

答案 0 :(得分:1)

您可以在Remote-Containers中使用lag

dplyr

library(dplyr) abc %>% group_by(country) %>% mutate(rate = count/lag(count)) # country date count rate # <fct> <fct> <int> <dbl> #1 A 2020-03-01 1 NA #2 A 2020-03-02 2 2 #3 A 2020-03-03 3 1.5 #4 B 2020-03-01 2 NA #5 B 2020-03-02 5 2.5 #6 B 2020-03-03 10 2 #7 C 2020-03-01 1 NA #8 C 2020-03-02 3 3 #9 C 2020-03-03 6 2 shift

data.table

或在基数R中使用library(data.table) setDT(abc)[, rate := count/shift(count), country] / head进行此操作。

tail

答案 1 :(得分:1)

使用dplyr

library(dplyr)
abc %>%
   group_by(country) %>%
   mutate(rate = c(NA, count[-n()]/count[-1]))

或使用data.table

library(data.table)
setDT(abc)[, rate := c(NA, count[.N]/count[-1]), country]