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=
This should work if your team
is either a matrix
or a data.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"
The basic idea is to get the indices of the "F" gender rows (using which
) and then use the set operation intersect
to AND it with your res
indices. There are also union
and setdiff
variants that can be useful at times.