2

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="F".

I need to do this in R and as I am a newbie to R, could not resolve this.

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"    
3
  • i need to get the names of those indices in vector res that map to "F" in gender. So for the above data, i need to extract "shr" as it is in vector res, and in matrix team at [16,] is "F" in gender.
    – pbd
    Apr 13, 2012 at 0:16
  • team[res,][team[res,]$genders=='F',] Apr 13, 2012 at 0:36
  • @AndresT - just to note - that solution will require conversion of the matrix to a data.frame first. Apr 13, 2012 at 0:51

3 Answers 3

7

There are many ways to do this.

You could first pick which rows are in res:

team$names[res]

Then you can pick which ones have gender being "F":

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

Note that team$genders[res] picks out the genders corresponding to the rows in res, and then you filter to only accept those that are female.


If you liked, you could do it the other way round:

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

Here team$genders=="F" is a logical vector of length nrow(team), being TRUE whenever the gender is "F" and FALSE otherwise.

The 1:nrow(team) generates row numbers, and 1:nrow(team) %in% res is TRUE if the row number is in res.

The & says "make sure that the gender is "F" AND the row number is in res".


You could even do which(team$genders=="F") which returns a vector of row numbers for females, and then do:

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

where the intersect picks row numbers that are present in both res and the females.


And I'm sure people with think of more ways.

5
  • I think the intersect() method is the cleanest and most readable solution, but it is nice to see other options. Apr 13, 2012 at 0:48
  • These all appear to be acting as if the team object is a data.frame where it was stated to be a matrix.
    – IRTFM
    Apr 14, 2012 at 4:00
  • Thanks DWin.. but i got error that $ is not for atomic vectors.. No idea what that means!
    – pbd
    Apr 14, 2012 at 14:58
  • @pdb - it means that your team is not a list or data.frame. $ does not work on numeric vectors or matrices, only on lists and other "recursive" objects. In my answer I use [ instead of $ which works on both.
    – Tommy
    Apr 14, 2012 at 21:36
  • Oh, I apologise. I didn't read your post properly and assumed you had a data frame (which allows dataframe$columnname) not a matrix ( matrix[,'columnname']). The logic of my answer is still the same but you'll have to convet $colname to [,'colname'] - you may prefer @Tommy's answer (you're welcome to change the "accepted answer" after the fact). Apr 15, 2012 at 6:22
7

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.

1
  • Sorry Tommy, didnt work, gave error:Error: subscript out of bounds
    – pbd
    Apr 13, 2012 at 0:17
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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.