我们的气象站每周记录每天的天气数据(大约7行/观察),我们每周收集一次疾病数据(每周一次观察/记录),我如何能加入 weather_df
最后一行的weather_df
,同时保持其他单元格的空白?我试过使用左join,但它错误地将一个数值从disation_df
添加到每周的所有天数,而不是在周末记录疾病数据。
可复制示例
weather_df <- structure(list(week = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("1", "2"), class = "factor"),
date = structure(c(1401062400, 1401148800, 1401235200, 1401321600,
1401408000, 1401494400, 1401580800, 1401667200, 1402272000,
1402358400, 1402444800, 1402531200, 1402617600, 1402704000,
1402790400, 1402876800), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
rainfall = c(0.8, 0, 1.4, 3, 0, 1, 0, 0, 3, 0, 2.4, 1.2,
0, 0, 0, 0), temperature = c(23.6, 21.9, 22.6, 20.1, 21.9,
20.3, 17.3, 15.5, 23.1, 22.4, 21.1, 20.4, 21.2, 21.5, 20.2,
20.4)), row.names = c(NA, -16L), class = c("tbl_df", "tbl",
"data.frame"))
disease_df <- structure(list(week = structure(1:2, levels = c("1", "2"), class = "factor"),
disease_intensity = c(0.4, 0.3)), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))
combine_df <- left_join(weather_df, disease_df, by = "week")
以下是输出
正如你所见,第1周的所有天数中加0.4,第2周的所有天数中加0.3,第2周的所有天数中加0.3,我只想在这两个星期的最后几天中加这些,同时保持其他单元格的空白。