R - How to remove elements in a vector that satisfy one of several conditions

I have a large framework containing a column of names, and given the nature of my data, the names are repeated. I also have a vector of a subset of those names that I need to exclude from this frame. Therefore, I want to determine the line number for each instance, which name in the data frame matches the name in the list of names to be deleted. Here is an example of what I'm trying to do ... but I can't get the code to work. Thanks!

a=c("tom", "bill", "sue", "jim", "tom", "amy") b=c(12,15,7,22,45,5) ab=data.frame(a,b) ab drop=which(ab$a==c("tom", "sue")) #only identifies those matching "tom" drop ab2=ab[-drop,] ab2 
+4
source share
2 answers

You are looking for %in%

 drop=which(ab$a %in% c("tom", "sue")) 

however, more succinctly:

 ab[!ab$a %in% c('tom', 'sue'),] 
+9
source

You should look at the sqldf package. You can perform SQL selections in R-frames of data.

+1
source

Source: https://habr.com/ru/post/1393020/


All Articles