Subsetting matrices

前端 未结 3 1552
遇见更好的自我
遇见更好的自我 2021-01-07 05:01

Considering following vector res and matrix team. the vector res represent indices, and I require to extract only those names whose index number is in vector res and gender=

3条回答
  •  别那么骄傲
    2021-01-07 05:35

    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"
    

提交回复
热议问题