Liam Gower
Liam Gower

Reputation: 324

Finding 'first occurrence' using Match Function in R

I am new to 'R' and 'Stackoverflow' so forgive me for the incredibly basic question. I'm trying to find the 'index' of the first female in my dataset.

Code Snapshot

My overall dataset is called 'bike', so first I thought it would be a good idea to assign a new vector of just the genders...

bike$genders

Then I tried using the function:

match(1, genders)
match(F, genders)

Neither of which worked! I know this is and should be relatively simple but I'm just starting out so I really appreciate your help.

Upvotes: 5

Views: 13115

Answers (2)

Kevin
Kevin

Reputation: 339

Probably the most direct method would be to use match("F", bike[,"genders"] which will return the index of the first match.

Upvotes: 7

sconfluentus
sconfluentus

Reputation: 4993

If you want to know the rows#, this should give you the rows, with their numbers printed to the screen, and you will see the index for rows with it.

bike[bike$gender=="F",]

and if you only want the row numbers to set to a vector
rnam<-row.names(bike[bike$gender=="F",])

Upvotes: 0

Related Questions