I need to extract parts of a dataframe, using the values which I have generated previously. For example, I have the following data:
a<-c(1,2,3,4,6,7,10,12,17,20)
df1<-data.frame(a)
I then want to exclude these values (in “a” in df1) from df2 when they appear in column b:
b<-c(1,2,3,4,5,6,6,6,7,8,9,10,11,11,11,12,13,14,14:20)
c<-c(1:25)
df1<-data.frame(b,c)
So, I should be left with a dataframe with rows 5,8,9,11 etc…
Can anyone help me out with the code to remove these values from my dataframe (df1).
Many thanks.
subset()will be a good friend to you for this sort of thing:(The sub-expression
b %in% atests each element ofbto determine whether or not it is ina, returning a vector ofTRUEs andFALSEes.!b %in% ajust negates/flips those Boolean values, so that you end up with a logical vector indexing withTRUEs the rows ofdf1that you would like to keep (i.e. those that don’t appear in a).)