I have a dataset with three columns (A, B, C). Each corresponds to the amount of Apple, Bananas or Carrots a child eats per day.
For each day I want to log the most consumed food (i.e., column with the highest value). If value between columns is equal (e.g., 1 apple and 1 banana), then apply the following rank. Apple > Banana > Carrot, whereby if a child eats 1 Apple and 1 Banana, the log will show Apple.
I've tried implementing this in R using by classifying pairs of if_else statements using case_when(). However, it does not return the correct result. For example, the final row should be classified as Apple, not carrot. I'm not sure what I'm doing wrong.
Note: Please provide a Tidyverse implemented solution rather than base R as this is how I'm learning R. Using a different function to the one I tried is fine but if possible, please explain why my attempt doesn't work so I can learn.
library(tidyverse)
A <- c(1,1,3,3)
B <- c(2,3,1,1)
C <- c(1,1,1,2)
df <- data.frame(A,B,C)
top_food <- df %>%
mutate(highest = case_when(
C > B ~ "carrot", # if carrot > banana
C > A ~ "carrot", # if carrot > apple
B > A ~ "banana", # if banana > apple
B >= A ~ "banana", # if banana >= carrot
A >= B ~ "apple", # if apple >= banana
A >= C ~ "apple" # if apple >= carrot
))
> | A | B | C | HIGHEST | |
> | 1 | 2 | 1 | banana | |
> | 1 | 3 | 1 | banana | |
> | 3 | 1 | 1 | apple | |
> 3 | 1 | 2 | carrot | |