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

心已入冬 提交于 2020-12-29 07:41:05

问题


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.


回答1:


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



回答2:


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



回答3:


Another variation on a solution using the ifelse() command:

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

output <-ifelse(data[,1] == "English" & data[,2] == 4, TRUE, FALSE)

> output
[1] FALSE FALSE FALSE  TRUE


来源:https://stackoverflow.com/questions/46299925/subsetting-a-data-frame-based-on-a-logical-condition-on-a-subset-of-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!