Santhosh Subramanian
Santhosh Subramanian

Reputation: 125

How to merge these two datasets in R

How do I Merge these two data sets so that the N/A's are filled.

Example ->

Name Abbreviation Denomination
-------------------------------
Asia     A             N/A
Bob      B             N/A
Chris    C             N/A
David    D             N/A

Merged With

Name Abbreviation Denomination
-------------------------------
Asia     A             5
Bradley  BA            1
Chris    C             7
David    D             9
Emma     EM            2
Soo      So            4
Nate     NT            2
Bob      B             1
Brat     B             5
Asia     S             2

How do I merge the two sets so I can get:

Name Abbreviation Denomination
-------------------------------
Asia     A             5
Bob      B             1
Chris    C             7
David    D             9

Upvotes: 0

Views: 86

Answers (2)

John_dydx
John_dydx

Reputation: 961

Try this:

x <- data.frame(Name = c("Asia", "Bradley", "Chris", "David", "Emma", "Soo", "Nate", 
                     "Bob", "Brat", "Asia"),
            Abb = c("A", "BA", "C", "D", "EM", "So", "NT", "B", "B", "S"),
            Den = c(5, 1, 7, 9, 2, 4, 2, 1, 5, 2))

y <- data.frame(Name = c("Asia", "Bob", "Chris", "David"), Abb = c("A","B", "C", "D"),
            Den = c("NA", "NA", "NA", "NA"))

data_df <- merge(x, y , by = 1)

subset(data_df, Abb.x != "S", c(1,2,3))


     Name Abb.x Den.x
  1  Asia     A     5
  3   Bob     B     1
  4 Chris     C     7
  5 David     D     9

You can always rename columns etc to whatever name you wish.

Upvotes: 0

SabDeM
SabDeM

Reputation: 7190

I think there is a smart way to obtain that, but here is my clumsy solution:

 df3 <- merge(df1, df2, by = c("Name", "Abbreviation"))
 df3
   Name Abbreviation Denomination.x Denomination.y
1  Asia            A            N/A              5
2   Bob            B            N/A              1
3 Chris            C            N/A              7
4 David            D            N/A              9

and then drop the column Denomination.x with df3 <- df3[, -3]

df3
   Name Abbreviation Denomination.y
1  Asia            A              5
2   Bob            B              1
3 Chris            C              7
4 David            D              9

Upvotes: 1

Related Questions