【问题标题】:R: Join and merge two data.frames [duplicate]R:加入并合并两个data.frames [重复]
【发布时间】: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)吗?

标签: r join merge dataframe


【解决方案1】:

我相信,这将给你想要的,只使用基地merge

df3 <- merge(df1,df2,by=c("dataName","Site"),all.x=TRUE,all.y=TRUE)


df3
  dataName Site add proxy   all
1       HA  df3   0  17.3  <NA>
2      Kol CA35   1  23.5  Test
3      hcd leta  NA    NA   ole
4       la holz  NA    NA dummy

请注意,由于您有不平衡的列并希望保留它们,因此您必须指定 all = true。

【讨论】:

    【解决方案2】:

    df

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 2012-12-15
      • 2019-03-28
      • 1970-01-01
      相关资源
      最近更新 更多