我们的气象站每周记录每日天气数据(约7行/观测)。我们每周收集一次疾病数据(每周一次观察/行)。我怎样才能加入的最后一排
weather_df
具有
disease_df
同时保持其他单元格为空白?我尝试过使用left_join,但它错误地从中添加了一个值
疾病_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")
这是输出
如您所见,0.4添加到第1周的所有日子,0.3添加到第2周的所有天。我只想把这些添加到两周的最后几天,同时保持其他单元格为空白。