-1

I have two different dataframes, df1 and df2. I need to delete the observations from df1 that are within df2. I was thinking of doing a loop through every row and column and check if the value is within df2 and if it is, delete it. I was wondering if maybe there is a faster way to do so. This is what I have until now

`for(i in 1:nrow(df1)){
 for(j in 1:ncol(df1)){
   if(df[i,j] %in% df2){
   df[i,j] <- 'NA'
   }
 }
}`

I do not want to delete the whole row, only the values that are in df2 and shift cells to the left. and then delete all the NA values. Thank you very much.

  • Please add reproducible data, desired result, and clarify what you mean by "delete all the NA values". Elements cannot be deleted inside data.frames. – Rich Scriven Jul 31 '17 at 20:58
  • @Frank This does not seem like a duplicate of the previous link...I have an answer which I think will satisfy @OP but cannot post due to the `duplicate` tag – CPak Jul 31 '17 at 21:27
  • @ChiPak You can lobby the OP to post a full example with desired output. Without that, I don't think this Q&A can be useful even with better answers. – Frank Jul 31 '17 at 21:35
  • @Santiago, I would post the same question as a separate question...I can provide a toy example. – CPak Jul 31 '17 at 21:40
  • It is different than the other one because I do not want to delete the whole row, I just want to delete the observations and shift cells to the left. – Santiago Veintimilla Jul 31 '17 at 21:49

1 Answers1

-1

Do you have any id or key to check?

You should be able to use an anti_join in the dplyr package, however, that checks the entire row from df1 to the entire row of df2:

?anti_join
"return all rows from x where there are not matching values in y, 
keeping just columns from x."
BigTimeStats
  • 447
  • 3
  • 12
  • Yeah, I think anti join is the cleanest way. "that checks the entire row from df1 to the entire row of df2" -- the `by=` argument allows for limiting the columns being compared, right? Anyway, I'm just downvoting because this should be fleshed out more. Also, if you have clarifying questions, they should be comments to the OP not part of an "answer". – Frank Jul 31 '17 at 20:41