2

I have a dataframe df_new that is a subset of a dataframe df_original, hence its ids are not sequential and are partial.

If I extract the IDs of this dataframe df_new, I get something like

8    9   23   24   25   26   27   28   29   30   34   35   39   40 2997   55   56   58  617  640   95   96 

How do I extract the numbers that are not in df_new, but in the original sequential ids from 1 to nrow(df_original) -- and hence the rows that are not in df_new but in df_original

Of course, I can create a for loop that checks if the numbers are in df_new$id, but I think in R there'd be a more elegant way to do this.

user1569897
  • 437
  • 1
  • 5
  • 12

1 Answers1

3
#DATA
ORIGINAL = c(1:10, 9:12)
SUBSET = c(2:5, 8)

I would use value matching (%in%) to identify elements in original list that are not present in the subset.

ORIGINAL[!ORIGINAL %in% SUBSET]
#[1]  1  6  7  9 10  9 10 11 12

You could also use setdiff but it will remove duplicates

setdiff(x = ORIGINAL, y = SUBSET)
#[1]  1  6  7  9 10 11 12
d.b
  • 32,245
  • 6
  • 36
  • 77