如何解决R中的lag函数无法按预期工作以计算每日值
我正在使用covid数据集,并尝试创建滞后值,我将进一步使用这些滞后值来计算每日病例,但是滞后不能按预期工作,并且不确定我要去哪里。
df
df_confirmed_gathered %>%
mutate(Cases_Dates = ymd(Cases_Dates)) %>%
group_by(Country.Region,Cases_Dates) %>%
filter(Country.Region == "Italy")
Country.Region Lat Long Cases_Dates Cases_Counts
<chr> <dbl> <dbl> <date> <int>
Italy 41.87194 12.56738 2020-02-01 2
Italy 41.87194 12.56738 2020-02-02 2
Italy 41.87194 12.56738 2020-02-03 2
Italy 41.87194 12.56738 2020-02-04 2
Italy 41.87194 12.56738 2020-02-05 2
Italy 41.87194 12.56738 2020-02-06 2
Italy 41.87194 12.56738 2020-02-07 3
Italy 41.87194 12.56738 2020-02-08 3
Italy 41.87194 12.56738 2020-02-09 3
Italy 41.87194 12.56738 2020-02-10 3
计算滞后时间
df_confirmed_gathered %>%
mutate(Cases_Dates = ymd(Cases_Dates)) %>%
group_by(Country.Region,Cases_Dates) %>%
mutate(lag_Cases = lag(Cases_Counts,default = 0)) %>%
filter(Country.Region == "Italy")
Country.Region Lat Long Cases_Dates Cases_Counts lag_Cases
<chr> <dbl> <dbl> <date> <int> <dbl>
Italy 41.87194 12.56738 2020-02-01 2 0
Italy 41.87194 12.56738 2020-02-02 2 0
Italy 41.87194 12.56738 2020-02-03 2 0
Italy 41.87194 12.56738 2020-02-04 2 0
Italy 41.87194 12.56738 2020-02-05 2 0
Italy 41.87194 12.56738 2020-02-06 2 0
Italy 41.87194 12.56738 2020-02-07 3 0
Italy 41.87194 12.56738 2020-02-08 3 0
Italy 41.87194 12.56738 2020-02-09 3 0
Italy 41.87194 12.56738 2020-02-10 3 0
使用滞后函数计算每日病例数
df_confirmed_gathered %>%
mutate(Cases_Dates = ymd(Cases_Dates)) %>%
group_by(Country.Region,Cases_Dates) %>%
mutate(Daily_Cases = Cases_Counts - lag(Cases_Counts,default = 0)) %>%
ungroup() %>%
filter(Country.Region == "Italy")
Country.Region Lat Long Cases_Dates Cases_Counts lag_Cases
<chr> <dbl> <dbl> <date> <int> <dbl>
Italy 41.87194 12.56738 2020-02-01 2 2
Italy 41.87194 12.56738 2020-02-02 2 2
Italy 41.87194 12.56738 2020-02-03 2 2
Italy 41.87194 12.56738 2020-02-04 2 2
Italy 41.87194 12.56738 2020-02-05 2 2
Italy 41.87194 12.56738 2020-02-06 2 2
Italy 41.87194 12.56738 2020-02-07 3 3
Italy 41.87194 12.56738 2020-02-08 3 3
Italy 41.87194 12.56738 2020-02-09 3 3
Italy 41.87194 12.56738 2020-02-10 3 3
解决方法
从Cases_Dates
中删除group_by
,并且滞后功能应该可以正常工作。如果您有多个Lat
和Long
值,那么显然您希望将它们添加到分组中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。