Looking up values in R

2019-06-03 23:43发布

问题:

I have the following data sets in R:

> closed
 [1] 57637 31508 54113  2348  4747 68284 61779 36654 31399 54126 11232 60659 42968 56388  5114 66289 69491 45668   879 65649 71172 36120 61802  7699 70982
[26] 65720

> length(closed)
[1] 26

> stlist[1:5,]
          M.Type      M.Code                 M.Name    B.Code               B.Name          B.Region
1             5M        1759            Istanbul 5M    110007               Taksim               Ist
2             5M       62461              Edirne 5M    110007            Aysekadin               Ist
3             5M       69493            Tekirdag 5M    110431             Tekirdag               Ist
4             5M        7516              Ankara 5M    110548             Batikent               Ank
5             5M       14148               Bursa 5M    110351                Bursa               Ist

> nrow(stlist)
[1] 1830

I want to look up all values of "closed" in "stlist$M.Code" and create a new data frame which only includes the rows with the 26 "M.Code"s with all 6 columns of "stlist". How can I do that?

回答1:

use %in% :

stlist <- data.frame(
    M.Code = letters[1:10],
    b = rnorm(10)
)
closed <- c("a","d","f")

stlist[stlist$M.Code %in% closed,]


回答2:

An alternative to Joris' answer is to use subset():

subset(stlist, subset = M.Code %in% closed)

The only major difference that I know of between the two is that subset() assumes that variables referenced in the criteria are a part of the data frame, if a variable with that name exists in the data frame. That's not much of a big deal in this example (just one extra 'stlist$'), but it saves a bit of typing if you have complex criteria.

Maybe somebody else knows of a more subtle difference.