简体   繁体   中英

Removing rows of data frame not satisfying a criteria in a specific column of the data frame (in R)

I sincerely apologize if this question have already been asked, but I've searched high and low and could not find any similar question.

I am working on a data frame in R, which is rather complex, so I shall short it down to the following data frame:

data<-data.frame(x=c("ENG","ITA","RUS","SLO"),y=c(4,3,6,4))
list.country<-list("ENG","RUS")

I have a function which creates a list, like "list.country" above, and whatever entries contained in this list, I want to extract the matching rows from the data.frame, into a new data frame. So, using the data and list above, I want to end up with a new data frame looking like this:

new.data<-data.frame(x=c("ENG","RUS",),y=c(4,6))

The only way I have found to do this, is extracting each row separately, and binding them together again afterwards, the following way:

vector.list<-unlist(list.country)
n=length(vector.list)
for(i in 1:n){
  data[[i]]<-data[data$x==vector.list[i],]

}
for(i in 2:n){data[[i]]<- rbind(data[[i-1]],data[[i]])} }

Does anyone have a simpler way to do this?

Thank you

Just do

data[data$x %in% list.country, ]

##     x y
## 1 ENG 4
## 3 RUS 6

The %in% function searches for all elements in first vector matching an element in the second one. In your case, you should do this

data[data$x %in% list.country, ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM