0

I have a data frame which comprises a subset of records contained in a 2nd data frame. I would like to add the record rows of the 2nd data frame that are not common in the first data frame to the first... Thank you.

Joseph
  • 9
  • 1
    Please read this: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and try to make your question reproducible – acylam Oct 12 '17 at 17:58

2 Answers2

0

You should look dplyr's distint() and bind_rows() functions. Or Better provide a dummy data to work on and expected output .

Suppose you have two dataframes a and b ,and you want to merge unique rows of a dataframe to the b dataframe

a = data.frame(
    x = c(1,2,3,1,4,3),
    y = c(5,2,3,5,3,3)
)
b = data.frame(
    x = c(6,2,2,3,3),
    y = c(19,13,12,3,1)
)
library(dplyr)
distinct(a) %>% bind_rows(.,b)
acylam
  • 18,231
  • 5
  • 36
  • 45
SumitArya
  • 111
  • 5
  • But my reputations isn't enought (required 50)to comment on other person post :( – SumitArya Oct 12 '17 at 17:28
  • 2
    Reproducible example by OP would be helpful, but meanwhile you can make your "comment" a full answer by giving more details, showing some code and making a dummy example like @Balter's. Not having enough rep is not a good reason to post a comment as an answer. – acylam Oct 12 '17 at 17:33
0

If you want all unique rows from both dataframes, this would work:

df1 <- data.frame(X = c('A','B','C'), Y = c(1,2,3))
df2 <- data.frame(X = 'A', Y = 1)

df <- rbind(df1,df2)
no.dupes <- df[!duplicated(df),]
no.dupes
#  X Y
#1 A 1
#2 B 2
#3 C 3

But it won't work if there's duplicate rows in either dataframe that you want to preserve.

Balter
  • 1,085
  • 6
  • 12