在R中查找值 [英] Looking up values in R

查看:152
本文介绍了在R中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有以下数据集:

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

我想查找所有在stlist $ M.Code中关闭,并创建一个新的数据帧,其中只包含具有26M.Code的行,其中所有6列为stlist。如何做到这一点?

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?

推荐答案

在%中使用%:

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

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

这篇关于在R中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆