-1

I have two sample 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

and 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

What I am trying is to merge the data.frames using a join so that the end result would look like this:

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

I tried all the variations from this post but I wasn't able to reach the desired end result using the merge() function. In my real data the data.frames are much bigger and contain more columns that one data.frame has and the other one doesn't. How could I solve this?

Community
  • 1
  • 1
nebuloso
  • 91
  • 7

2 Answers2

1

This, I believe, will give you want you want, using only base 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

Note that because you have imbalanced columns and want to keep them, you have to specify that all = true.

bjoseph
  • 2,116
  • 17
  • 24
0

df <- merge(df1, df2, by = c('dataName', 'Site'), all = T)

Panchito
  • 337
  • 1
  • 3
  • 12