Working with R, I am trying to create a variable, based on 3 existing variables in a data frame. Example:
n <- 20
dat <- data.frame(id=1:n,
group=rep(LETTERS[1:5]),
type=factor(paste("type", 1:2)))
The values of the new variable "model" should be based on those 3:
If "type" = 1 then "model"= apple
If "type" = 2 and "group" A or B, then "model"=cherry
If "type" = 2 and "group" C and "id" 1-4, then"model"= strawberry
If "type" = 2 and "group" C and "id" 5-8, then"model"= melon
If "type" = 2 and "group" C and "id" >9, then "model"=pineapple
I have tried with ifelse, but I struggle with the conditions.
How to solve this?