Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过在R中分组条目来创建时间序列?_R_Count_Group By_Time Series - Fatal编程技术网

如何通过在R中分组条目来创建时间序列?

如何通过在R中分组条目来创建时间序列?,r,count,group-by,time-series,R,Count,Group By,Time Series,我想创建一个时间序列,从2004年1月1日到2010年12月31日,R中的每日死亡率数据。我现在拥有的原始数据(.csv文件)以日-月-年为列,每一行都是死亡案例。因此,如果某一天的死亡率等于四,那么该日期有四行。如果某一天没有死亡病例报告,则数据集中会忽略该天 我需要的是一个2557行的时间序列(从2004年1月1日到2010年12月31日),其中列出了每天的死亡病例总数。如果某一天没有死亡案例,我仍然需要在列表中指定一个“0” 有人知道怎么做吗 谢谢, 戈西亚 原始数据的示例: day mo

我想创建一个时间序列,从2004年1月1日到2010年12月31日,R中的每日死亡率数据。我现在拥有的原始数据(.csv文件)以日-月-年为列,每一行都是死亡案例。因此,如果某一天的死亡率等于四,那么该日期有四行。如果某一天没有死亡病例报告,则数据集中会忽略该天

我需要的是一个2557行的时间序列(从2004年1月1日到2010年12月31日),其中列出了每天的死亡病例总数。如果某一天没有死亡案例,我仍然需要在列表中指定一个“0”

有人知道怎么做吗

谢谢, 戈西亚

原始数据的示例:

day month   year
1   1   2004
3   1   2004
3   1   2004
3   1   2004
6   1   2004
7   1   2004
我需要的是:

day month   year    deaths
1   1   2004    1
2   1   2004    0
3   1   2004    3
4   1   2004    0
5   1   2004    0
6   1   2004    1

df您应该添加示例数据。@罗兰-非常感谢!正是我需要的,Gosia@Gosia请随意勾选此答案左上角的复选标记。这让人们知道,你的问题已经得到了满意的回答。
df <- read.table(text="day month   year
1   1   2004
3   1   2004
3   1   2004
3   1   2004
6   1   2004
7   1   2004",header=TRUE)

#transform to dates
dates <- as.Date(with(df,paste(year,month,day,sep="-")))

#contingency table
tab <- as.data.frame(table(dates))
names(tab)[2] <- "deaths"
tab$dates <- as.Date(tab$dates)

#sequence of dates
res <- data.frame(dates=seq(from=min(dates),to=max(dates),by="1 day"))
#merge
res <- merge(res,tab,by="dates",all.x=TRUE)
res[is.na(res$deaths),"deaths"] <- 0
res
#       dates deaths
#1 2004-01-01      1
#2 2004-01-02      0
#3 2004-01-03      3
#4 2004-01-04      0
#5 2004-01-05      0
#6 2004-01-06      1
#7 2004-01-07      1