Consider the following data
F1 <- c(1,1,1,0,1)
F2 <- c(10,20,15,7,20)
F3 <- c('A', 'D', 'B', 'A', 'A')
F4 <- c(9,6,20,20,20)
F5 <- c(2,1,21,8,7)
df1 <- data.frame(F1,F2,F3,F4,F5)
When df1$F1==1
I want to obtain the max between $F4
, $F5
and $F2
, but only consider $F2
if the $F3
factor is A
or B
. Otherwise write NA
df1$max <- with(df1, ifelse(F1==1, pmax(F2[F3_condition],F4,F5), NA))
How can one account for the F3_condition
where er consider on factor A
or B
?
So $max
will take the following values: c(10,6,21,NA,20)
I have reviewed a similar question, but it does not exactly deal with the specific condition I require.