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?
CodePudding user response:
You can use case_when
from the tidyverse:
library(tidyverse)
n <- 20
dat <- data.frame(id=1:n,
group=rep(LETTERS[1:5]),
type=factor(paste("type", 1:2)))
dat %>%
mutate(
model = case_when(
type == "type 1" ~ "apple",
type == "type 2" & group %in% c("A", "B") ~ "cherry",
type == "type 2" & group == "C" & id %in% c(1:4) ~ "strawberry",
type == "type 2" & group == "C" & id %in% c(5:8) ~ "melon",
type == "type 2" & group == "C" & id >9 ~ "pineapple"
)
)
CodePudding user response:
Nested ifelse
option:
dat$color <- with(dat, ifelse(type == "type 1", "apple",
ifelse(type == "type 2" & group %in% c("A", "B"), "cherry",
ifelse(type == "type 2" & group == "C" & id %in% (1:4), "strawberry",
ifelse(type == "type 2" & group == "C" & id %in% (5:8), "melon",
ifelse(type == "type 2" & group == "C" & id >= 9, "pineapple", NA))))))
Output:
id group type color
1 1 A type 1 apple
2 2 B type 2 cherry
3 3 C type 1 apple
4 4 D type 2 <NA>
5 5 E type 1 apple
6 6 A type 2 cherry
7 7 B type 1 apple
8 8 C type 2 melon
9 9 D type 1 apple
10 10 E type 2 <NA>
11 11 A type 1 apple
12 12 B type 2 cherry
13 13 C type 1 apple
14 14 D type 2 <NA>
15 15 E type 1 apple
16 16 A type 2 cherry
17 17 B type 1 apple
18 18 C type 2 pineapple
19 19 D type 1 apple
20 20 E type 2 <NA>