2012-04-12 139 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"