R 子集矩阵

R 子集矩阵,r,R,考虑以下向量res和矩阵团队。 vector 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"

考虑以下向量res和矩阵团队。 vector 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"    

如果您的
团队
是一个
矩阵
数据框架
,则此功能应能正常工作:

# 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"
#模拟您的数据

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

您可以首先选择
res
中的行:

team$names[res]
然后,您可以选择哪些人的性别为“F”:

请注意,
team$genders[res]
会选择与
res
中的行相对应的性别,然后进行筛选,以仅接受女性


如果你喜欢,你可以反过来做:

team$names[  team$genders=="F" & (1:nrow(team) %in% res) ] 
这里,
team$genders==“F”
是长度的逻辑向量
nrow(team)
,当性别为“F”或
FALSE时,为
TRUE

1:nrow(团队)
生成行号,如果行号在
res
中,则%res
中的
1:nrow(团队)%为
TRUE

&
上写着“确保性别为“F”,行号在
res


您甚至可以执行
which(team$genders==“F”)
操作,返回女性的行号向量,然后执行以下操作:

team$names[ intersect(  which(team$genders=="F") , res ) ]
其中,
相交
选择同时出现在
res
和内螺纹中的行号



我相信,有这种想法的人会想出更多的方法。

团队我需要得到向量res中那些在性别中映射到“F”的索引的名称。所以对于上面的数据,我需要提取向量res中的“shr”,在矩阵team中[16]是性别中的“F”。team[res,][team[res,]$genders=='F',]@AndresT-只需注意-解决方案需要首先将矩阵转换为data.frame。抱歉,Tommy没有工作,给出错误:下标超出边界我认为
intersect()
方法是最干净、可读性最强的解决方案,但很高兴看到其他选项。这些选项看起来都像团队对象是一个data.frame,声明为矩阵。谢谢。。但我有个错误,$不是原子向量。。不知道那是什么意思@pdb-这意味着您的
团队
不是列表或数据框<代码>$
不适用于数值向量或矩阵,仅适用于列表和其他“递归”对象。在我的回答中,我使用了
[
而不是
$
,两者都适用。哦,我很抱歉。我没有正确阅读你的帖子,并假设你有一个数据框(允许
数据框$columnname
)而不是一个矩阵(
矩阵[,'columnname')
)。我的答案的逻辑仍然相同,但您必须将
$colname
转换为
[,'colname']
-您可能更喜欢@Tommy的答案(欢迎您在事后更改“已接受的答案”)。
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"
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"