df1 = data.frame(Id = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)), Test = NA)
df2 = data.frame(Id = c(2, 4, 6, 7), State = c(rep("Alabama", 2), rep("Ohio", 2)))
df_sum <- anti_join(df1,df2, by = "Id") %>% bind_rows(df2)
Is there an easy way to make the result of the anti_join so that only rows of df1 are present in the result. So for example the row with the Id 7 is dropped in the resulting dataframe. I can only think of laborious solutions. Thanks in advance.
The resulting table should look like this:
That's a strange result that you expect but I don't know what your plan is, so this line of code will give you exactly the table you want:
anti_join(df1,df2, by = "Id") %>% bind_rows(df2) %>% filter(Id %in% df1$Id)
I've just added a filter so that only the Id
s present inf df1
are retained.