试图创建每月计数,但用润滑剂收到错误
我正在处理时间序列数据,并尝试在多年内计算特定事件。 我想创建一个新的.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论