這應該工作,
date_seq <- seq(min(df$the_day),max(df$the_day),by = 1)
rbind(df,cbind(the_day = as.character(date_seq[!date_seq %in% df$the_day]), inf = "0", specified = "0", both = "0"))
# the_day inf specified both
# 1 2015-11-02 1.32 156 157.32
# 2 2015-11-04 4.25 40 44.25
# 3 2015-11-05 3.25 25 28.25
# 4 2015-11-06 1 15 16
# 5 2015-11-07 4.75 10 14.75
# 6 2015-11-08 32 0 32
# 7 2015-11-03 0 0 0
如果你想對它進行排序,根據the_day
,參加一個變量的數據幀,並使用order
功能
ans <- rbind(df,cbind(the_day = as.character(date_seq[!date_seq %in% df$the_day]), inf = "0", specified = "0", both = "0"))
ans[order(ans$the_day), ]
# the_day inf specified both
# 1 2015-11-02 1.32 156 157.32
# 7 2015-11-03 0 0 0
# 2 2015-11-04 4.25 40 44.25
# 3 2015-11-05 3.25 25 28.25
# 4 2015-11-06 1 15 16
# 5 2015-11-07 4.75 10 14.75
# 6 2015-11-08 32 0 32
請'dput'數據組。 – nrussell
並且期望輸出 – Minnow
結構(列表(the_day = structure(c(16742,16743,16744,16745, 16746,16747),class =「Date」),inf = c(「1.32」,「4.25」,「 3.25「, 」1「,」4.75「,」32「),指定= c(」156「,」40「,」25「,」15「,」10「, 」0「 (「157.32」,「44.25」,「28.25」,「16」,「14.75」,「32」 )),.Names = c(「the_day」,「inf」,「specified」,「both」 row.names = c(NA, 6L),class =「data.frame」) – George