clrp
clrp

Reputation: 21

Add rows to dataframe from another dataframe, based on a vector

I'd like to add rows to a dataframe based on a vector within the dataframe. Here are the dataframes (df2 is the one I'd like to add rows to; df1 is the one I'd like to take the rows from):

ID=c(1:5)
x=c(rep("a",3),rep("b",2))
y=c(rep(0,5))
df1=data.frame(ID,x,y)
df2=df1[2:4,1:2]
df2$y=c(5,2,3)

df1
  ID x y
1  1 a 0
2  2 a 0
3  3 a 0
4  4 b 0
5  5 b 0

df2
  ID x y
2  2 a 5
3  3 a 2
4  4 b 3

I'd like to add to df2 any rows that aren't in df1, based on the ID vector. so my output dataframe would look like this:

 ID x y
  1 b 0
  5 b 0
  2 a 5
  3 a 2
  4 b 3

Can anyone see a way of doing this neatly, please? I need to do it for a lot of dataframes, all with different numbers of rows. I've tried using merge or rbind but I haven't been able to work out how to do it based on the vector.

Thank you!

Upvotes: 1

Views: 4527

Answers (3)

Nicolas2
Nicolas2

Reputation: 2210

A solution with dplyr:

bind_rows(df2,anti_join(df1,df2,by="ID"))

#  ID x y
#1  2 a 5
#2  3 a 2
#3  4 b 3
#4  1 a 0
#5  5 b 0

Upvotes: 3

alex_555
alex_555

Reputation: 1102

What about this one-liner?

rbind(df2, df1[!df1$ID %in% df2$ID,])

  ID x y
2  2 a 5
3  3 a 2
4  4 b 3
1  1 a 0
5  5 b 0

Upvotes: 1

gaut
gaut

Reputation: 5958

You can do the following:

missingIDs <-  which(!df1$ID %in% df2$ID) #check which df1 ID's are not in df2, see function is.element()
df.toadd <- df1[missingIDs,] #define the data frame to add to df2
result <- rbind(df.toadd, df2) #use rbind to add it
result

  ID x y
1  1 a 0
5  5 b 0
2  2 a 5
3  3 a 2
4  4 b 3

Upvotes: 1

Related Questions