Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subsetting a data frame based on a logical condition on a subset of rows

I've tried to come up with a simple solution to the followig problem. Here is the minimum working example:

data <- data.frame(subject = c('Math', 'English', 'French', 'English'),
                   grade = c(1, 3, 5, 4))

I want a function that compares Enlish grades and returns a logical vector that has TRUE for the row with the highest english grade, and FALSE for all other rows. In this case [1] FALSE FALSE FALSE TRUE.

like image 526
RobustTurd Avatar asked Dec 19 '25 20:12

RobustTurd


2 Answers

We can get the max 'grade' per 'subject' with ave compare it with the 'grade' to get a logical index and check whether the 'subject' is also 'English'

with(data, ave(grade, subject, FUN = max)==grade & subject == "English") 
#[1] FALSE FALSE FALSE  TRUE
like image 183
akrun Avatar answered Dec 21 '25 11:12

akrun


Using an ifelse condition, one way would be the following.

library(dplyr)

data %>%
mutate(check = if_else(subject == "English" & grade == max(grade[subject == "English"]),
       TRUE,
       FALSE))

#  subject grade check
#1    Math     1 FALSE
#2 English     3 FALSE
#3  French     5 FALSE
#4 English     4  TRUE
like image 26
jazzurro Avatar answered Dec 21 '25 11:12

jazzurro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!