试图创建每月计数,但用润滑剂收到错误

发布于 2025-01-25 14:16:30 字数 976 浏览 3 评论 0原文

我正在处理时间序列数据,并尝试在多年内计算特定事件。 我想创建一个新的.dataframe,以显示每个月/年的事件总数,其中包括没有活动的数月的0个。

以下代码适用于一组数据,但不适用于两个完全相同的剩余数据集。

返回错误:

seq.int中的错误(r1 $ mon,12 *(to0 $ year -r1 $ ear) + to0 $ mon,by):
“来自”必须是有限的数字

代码:

new.dataframe <- original.dataframe %>%      
  group_by(var1, var2, .drop = FALSE) %>% 
  drop_na(date_var) %>% 
  mutate(month = lubridate::floor_date(date_var, unit = "months")) %>%  
  count(month) %>%                          
  complete(
    month = seq.Date(
      min(month, na.rm=T),     # include all months with no cases reported
      max(month, na.rm=T),
      by="month"),
    fill = list(n = 0))
new.dataframe <- new.dataframe %>% rename (monyear = month)
new.dataframe$monyear <- format(new.dataframe$monyear, "%m/%Y")

如果我删除以下行,则代码运行正常,但是我想要这些变量:

group_by(var1, var2, .drop = FALSE) %>% 

任何人对该行的代码有任何建议吗?

I am working with time series data and trying to count particular events across multiple years.
I want to create a new.dataframe that shows the total count of events for each month/year, including 0 for months that have no events.

The following code worked for one set of data but is not working for two remaining datasets that are set up exactly the same.

Error returned:

Error in seq.int(r1$mon, 12 * (to0$year - r1$year) + to0$mon, by) :
'from' must be a finite number

Code:

new.dataframe <- original.dataframe %>%      
  group_by(var1, var2, .drop = FALSE) %>% 
  drop_na(date_var) %>% 
  mutate(month = lubridate::floor_date(date_var, unit = "months")) %>%  
  count(month) %>%                          
  complete(
    month = seq.Date(
      min(month, na.rm=T),     # include all months with no cases reported
      max(month, na.rm=T),
      by="month"),
    fill = list(n = 0))
new.dataframe <- new.dataframe %>% rename (monyear = month)
new.dataframe$monyear <- format(new.dataframe$monyear, "%m/%Y")

The code runs fine if I remove the following line, however I want these variables:

group_by(var1, var2, .drop = FALSE) %>% 

Does any one have any suggestions for that line of code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文