【发布时间】:2015-08-10 16:11:06
【问题描述】:
我有两个示例 data.frames df1
df1 <- structure(list(dataName = structure(c(2L, 1L), .Label = c("HA", "Kol"), class = "factor"), Site = structure(1:2, .Label = c("CA35", "df3"), class = "factor"), add = c(1L, 0L), proxy = c(23.5, 17.3)), .Names = c("dataName", "Site", "add", "proxy"), class = "data.frame", row.names = c(NA, -2L))
df1
dataName Site add proxy
1 Kol CA35 1 23.5
2 HA df3 0 17.3
和df2
df2 <- structure(list(dataName = structure(c(2L, 1L, 3L), .Label = c("hcd", "Kol", "la"), class = "factor"), Site = structure(c(1L, 3L, 2L), .Label = c("CA35", "holz", "leta"), class = "factor"), all = structure(c(3L, 2L, 1L), .Label = c("dummy", "ole", "Test"), class = "factor")), .Names = c("dataName", "Site", "all"), class = "data.frame", row.names = c(NA, -3L))
df2
dataName Site all
1 Kol CA35 Test
2 hcd leta ole
3 la holz dummy
我正在尝试使用连接合并 data.frames,以便最终结果如下所示:
df_new
dataName Site add proxy all
1 Kol CA35 1 23.5 Test
2 HA df3 0 17.3 <NA>
3 hcd leta NA NA ole
4 la holz NA NA dummy
我尝试了this 帖子中的所有变体,但我无法使用merge() 函数达到预期的最终结果。在我的真实数据中,data.frames 要大得多,并且包含一个 data.frame 具有而另一个没有的列更多。我怎么能解决这个问题?
【问题讨论】:
-
你试过
merge(df1, df2, by=c("dataName", "Site"), all=T)吗?