我有一个数据集,包括年和月的案例.缺少几个月,我想创建那些月份的案例数为零的行.
这是一个例子,我目前的蛮力方法.谢谢你的任何指示.显然,我是新手.
# fake data
library(plyr)
rm(FakeData)
FakeData <- data.frame(DischargeYear=c(rep(2010, 7), rep(2011,7)),
DischargeMonth=c(1:7, 3:9),
Cases=trunc(rnorm(14, mean=100, sd=20)))
# FakeData is missing data for some year/months
FakeData
# Brute force attempt to add rows with 0 and then total
for(i in 1:12){
for(j in 1:length(unique(FakeData$DischargeYear))){
FakeData <- rbind(FakeData, data.frame(
DischargeYear=unique(FakeData$DischargeYear)[j],
DischargeMonth=i,
Cases=0))
}
}
FakeData <- ddply(FakeData, c("DischargeYear","DischargeMonth"), summarise, Cases=sum(Cases))
# FakeData now has every year/month represented
FakeData
Run Code Online (Sandbox Code Playgroud)
使用您的FakeData数据框,试试这个:
# Create all combinations of months and years
allMonths <- expand.grid(DischargeMonth=1:12, DischargeYear=2010:2011)
# Keep all month-year combinations (all.x=TRUE) and add in 'Cases' from FakeData
allData <- merge(allMonths, FakeData, all.x=TRUE)
# 'allData' contains 'NA' for missing values. Set them to 0.
allData[is.na(allData)] <- 0
# Print results
allData
Run Code Online (Sandbox Code Playgroud)