我有两个数据帧,例如:
head (df1)
Date v1 v2 v3
2007-01-01 1 1 Yes
2007-01-02 2 1 Yes
2007-01-03 2 1 Yes
2007-01-04 12 1 Yes
2007-01-05 3 1 Yes
和:
head (df2)
Date v1 v2 v3
2007-01-01 1 1 No
2007-01-01 2 1 No
2007-01-04 12 1 No
2007-01-05 3 1 No
如果df2
到df1
的行具有相同的日期,我只想添加它们。最后的df应该是这样的:
Date v1 v2 v3
2007-01-01 1 1 Yes
2007-01-01 1 1 No
2007-01-01 2 1 No
2007-01-02 2 1 Yes
2007-01-03 2 1 Yes
2007-01-04 12 1 Yes
2007-01-04 12 1 No
2007-01-05 3 1 Yes
2007-01-05 3 1 No
I'm sure there is a simple way to do this, I just can't quite find it.
我们可以根据"df1"和rbind
中的"Date"列,对"df2"中的"Date"进行子集设置
out <- rbind(df1, subset(df2, Date %in% df1$Date))
out[order(out$Date),]
-输出
Date v1 v2 v3
1 2007-01-01 1 1 Yes
6 2007-01-01 1 1 No
7 2007-01-01 2 1 No
2 2007-01-02 2 1 Yes
3 2007-01-03 2 1 Yes
4 2007-01-04 12 1 Yes
8 2007-01-04 12 1 No
5 2007-01-05 3 1 Yes
9 2007-01-05 3 1 No
或使用dplyr
library(dplyr)
bind_rows(df1, filter(df2, Date %in% df1$Date)) %>%
arrange(Date)
数据
df1 <- structure(list(Date = c("2007-01-01", "2007-01-02", "2007-01-03",
"2007-01-04", "2007-01-05"), v1 = c(1L, 2L, 2L, 12L, 3L), v2 = c(1L,
1L, 1L, 1L, 1L), v3 = c("Yes", "Yes", "Yes", "Yes", "Yes")),
class = "data.frame", row.names = c(NA,
-5L))
df2 <- structure(list(Date = c("2007-01-01", "2007-01-01", "2007-01-04",
"2007-01-05"), v1 = c(1L, 2L, 12L, 3L), v2 = c(1L, 1L, 1L, 1L
), v3 = c("No", "No", "No", "No")),
class = "data.frame", row.names = c(NA,
-4L))