2012-04-12 95 views
2

考虑以下矢量资源和矩阵团队。 矢量res代表索引,我只需要提取那些索引号在vector res和gender =“F”中的名字。子集矩阵

我需要在R中这样做,因为我是R的新手,无法解决这个问题。

res 
[1] 2 12 16 5 6 19 17 14 9 4 
team 
names  genders 
[1,] "aa"   "M"  
[2,] "ab"   "M"  
[3,] "al"   "M"  
[4,] "alp"   "M"  
[5,] "amr"   "F"  
[6,] "and"   "M"  
[7,] "an"   "M"  
[8,] "anv"   "F"  
[9,] "as"   "M"  
[10,] "ed"   "M"  
[11,] "neh"   "F"  
[12,] "pan"   "M"  
[13,] "poo"   "F"  
[14,] "ra"   "M"  
[15,] "roh"   "M"  
[16,] "shr"   "F"  
[17,] "sub"   "M"  
[18,] "val"   "M"  
[19,] "xi"   "M"  
+0

我需要得到在矢量水库中的性别映射到“F”的那些指数的名称。因此,对于上述数据,我需要提取“shr”,因为它在矢量水库中,在[16,]矩阵团队中,性别是“F”。 – pbd 2012-04-13 00:16:35

+0

团队[res,] [团队[res,] $性别=='F',] – aatrujillob 2012-04-13 00:36:30

+0

@AndresT - 请注意 - 解决方案需要先将矩阵转换为data.frame。 – thelatemail 2012-04-13 00:51:22

回答

7

有很多方法可以做到这一点。

你可以先挑这行是在res

team$names[res] 

然后你就可以挑选哪些具有gender"F"

team$names[res][ team$genders[res]=="F" ] 

注意team$genders[res]挑选出对应于行的性别在res,然后你过滤到只接受那些女性。


如果你喜欢,你可以轮做它的其他方式:

team$names[ team$genders=="F" & (1:nrow(team) %in% res) ] 

这里team$genders=="F"是长度nrow(team)的逻辑载体,是TRUE每当性别是“F”和FALSE否则。

1:nrow(team)生成行号,如果行号在res中,则1:nrow(team) %in% resTRUE

&说“确保性别是”F“,行号是res”。


你甚至可以做which(team$genders=="F")返回女性行数的向量,然后执行:

team$names[ intersect( which(team$genders=="F") , res) ] 

其中intersect选秀权行号存在于res和女性。


而且我敢肯定,人们会想到更多的方式。

+0

+ +1为皮肤的猫提供了很多方法:) – Tommy 2012-04-13 00:26:43

+0

我认为'intersect()'方法是最干净和最可读的解决方案,但很高兴看到其他选项。 – thelatemail 2012-04-13 00:48:30

+0

这些看起来好像团队对象是一个data.frame,它被称为矩阵。 – 2012-04-14 04:00:28

7

这应该工作,如果你的team要么是matrixdata.frame

# emulate your data 
team <- data.frame(names=LETTERS, genders=rep(c("M","F"), 13)) 
res <- 10:26 

team[intersect(res, which(team[,"genders"]=="F")), "names"] 
#[1] J L N P R T V X Z 
#Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 

# Try with a matrix instead of data.frame 
team <- as.matrix(team) 
team[intersect(res, which(team[,"genders"]=="F")), "names"] 
#[1] "J" "L" "N" "P" "R" "T" "V" "X" "Z" 

的基本思想是让“F”性别行的索引(使用which),然后使用设置操作intersect与您的res指数进行比较。也有unionsetdiff变种,有时可能有用。

+0

对不起汤米,没有工作,给错误:错误:下标越界 – pbd 2012-04-13 00:17:47

+0

@PankajDeshmukh - 现在它应该工作... – Tommy 2012-04-13 00:18:15

+0

@汤米 - 它没有...谢谢..! – pbd 2012-04-14 14:58:58

2
team <- structure(c("aa", "ab", "al", "alp", "amr", "and", "an", "anv", 
"as", "ed", "neh", "pan", "poo", "ra", "roh", "shr", "sub", "val", 
"xi", "M", "M", "M", "M", "F", "M", "M", "F", "M", "M", "F", 
"M", "F", "M", "M", "F", "M", "M", "M"), .Dim = c(19L, 2L), .Dimnames = list(
    NULL, c("names", "genders"))) 

team[,"names"][ intersect( which(team[,"genders"]=="F") , res) ] 
#[1] "amr" "shr" 
team[,"names"][ team[,"genders"]=="F" & 1:NROW(team) %in% res ] 
#[1] "amr" "shr"