我正在合并两个数据集RH.data和CC(见下文)。
>head(RH.data)
date RH
1 2005-05-01 71.1
2 2005-05-02 47.0
3 2005-05-03 58.6
4 2005-05-04 44.2
5 2005-05-05 41.8
6 2005-05-06 61.3
> head(cc)
X id date case year month temp
1 1 2005-05-01 1 2005 5 98
2 1 2005-05-02 0 2005 5 62
3 1 2005-05-05 0 2005 5 78
4 2 2005-05-01 1 2005 5 64
5 2 2005-05-06 0 2005 5 75
6 2 2005-05-04 0 2005 5 98
7 2 2005-05-02 0 2005 5 62
8 3 2005-05-03 1 2005 5 88
我正在尝试使用代码
按日期合并它们 merge(CC, RH.data, by="date", all=T)
但是,当我运行此代码时,日期会更改,我的数据将替换为NA
date X id case year month temp RH
1 12904 NA NA NA NA NA NA 71.1
2 12905 NA NA NA NA NA NA 47.0
3 12906 NA NA NA NA NA NA 58.6
4 12907 NA NA NA NA NA NA 44.2
我需要CC的顺序保持不变,只需要输入RH的值来输入日期相同的地方。什么代码可以让我这样做?
答案 0 :(得分:0)
使用tidyverse软件包尝试此解决方案:
library(tidyverse)
date = c("2005-05-01", "2005-05-02", "2005-05-03", "2005-05-04", "2005-05-05", "2005-05-06")
RH = c(71.1, 47.0, 58.6, 44.2, 41.8, 61.3)
RH <- data_frame(date, RH)
X <- 1:8
id <- c(1, 1, 1, 2, 2, 2, 2, 3)
date <- c("2005-05-01", "2005-05-02", "2005-05-05", "2005-05-01", "2005-05-06", "2005-05-04", "2005-05-02", "2005-05-03")
case <- c(1, 0, 0, 1, 0, 0, 0, 1)
year <- 2005
month <- 5
temp <- c(98, 62, 78, 64, 75, 98, 62, 88)
cc <- data_frame(X, id, date, case, year, month, temp)
joined <- right_join(RH, cc, by = "date")
您还应该考虑&#34;整理&#34;您的数据集,请参阅http://tidyr.tidyverse.org。