user2983722
user2983722

Reputation: 215

R: Counting number of a given category if a condition satisfies

I have been stumbled in a part of a question.

  id <-1:6
  age <- c(18,20,19,23,26,24)
  gender <- c("M","M","F","F","M","F")
  gpa <- c(3.2,2.5,2.7,3.7,4.1,3.6)
  score <- c(570,530,550,650,690,640)

  data <- data.frame(id=id,gender=gender,gpa=gpa,ceescore=score) 

  data$class <- with(data,ifelse(gpa>3.5,"First Class",
                           ifelse(gpa>=2.5 & gpa<=3.5,"Second class",
                             ifelse(gpa<2.5,"Third class",NA))))

  data$final_score <- with(data,ifelse(gpa>3,ceescore*gpa,ceescore*gpa*0.8))

  data$success <- with(data,ifelse(final_score>2000,1,0))

Now i have to count the number of female(in gender F for female) who are successful(success==1)

I couldn't able to manage it.

Thank's in advance.

Upvotes: 0

Views: 171

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81693

with(data, sum(gender == "F" & success))

will do the trick.

Upvotes: 1

Related Questions