在 R 中查找值 [英] Looking up values in R
本文介绍了在 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"中查找close"的所有值并创建一个新的数据框,该数据框仅包含具有 26 个M.Code"的行以及stlist"的所有 6 列.我该怎么做?
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?
推荐答案
使用 %in% :
stlist <- data.frame(
M.Code = letters[1:10],
b = rnorm(10)
)
closed <- c("a","d","f")
stlist[stlist$M.Code %in% closed,]
这篇关于在 R 中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文