Finding rows based on condition

I'm new on R and am having some trouble on that specific query

I want to find the people that have type 1 before (order) having a type 2 (and they must have both types)

Person | type | order
A1     | 1    | 1
A1     | 1    | 2 
A2     | 2    | 3
A2     | 1    | 4
A2     | 2    | 5 
A3     | 1    | 6
A4     | 1    | 7
A4     | 2    | 8
A5     | 1    | 9
A5     | 1    | 10
A5     | 2    | 11

In this example I'd have A4 and A5 as an answer.

Upvotes: 1

Views: 46

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

You could create a flag to identify the Person where type == 1 occurs before type == 2.

library(dplyr)

df %>%
  group_by(Person) %>%
  summarise(flag = which.max(type == 1) < which.max(type == 2))

# Person     flag 
#  <fct>     <lgl>
#1  A1      FALSE
#2  A2      FALSE
#3  A3      FALSE
#4  A4      TRUE 
#5  A5      TRUE 

Using the same logic with base R aggregate

df1 <- aggregate(type~Person, df, function(x) which.max(x == 1) < which.max(x == 2))

df1
# Person  type
#1 A1      FALSE
#2 A2      FALSE
#3 A3      FALSE
#4 A4       TRUE
#5 A5       TRUE

You could then subset type to get People

df1$Person[df1$type]
#[1] "A4"  "A5"

Another approach using base R ave

unique(df$Person[as.logical(with(df, 
   ave(type, Person, FUN = function(x) which.max(x == 1) < which.max(x == 2))))])
#[1] "A4" "A5"

Upvotes: 1

Related Questions