Mislabeled variable types cont.
age
is a character variable because there’s afive
instead of5
.As seen below, we can use
if_else(test, yes, no)
to say ifage
is the character string"five"
, make it"5"
, and if not leave it asage
.
students <- students |>
janitor::clean_names() |>
mutate(
meal_plan = factor(meal_plan),
age = parse_number(if_else(age == "five", "5", age))
)
students
## # A tibble: 6 × 5
## student_id full_name favourite_food meal_plan age
## <dbl> <chr> <chr> <fct> <dbl>
## 1 1 Sunil Huffmann Strawberry yoghurt Lunch only 4
## 2 2 Barclay Lynn French fries Lunch only 5
## 3 3 Jayendra Lyne <NA> Breakfast and lunch 7
## 4 4 Leon Rossini Anchovies Lunch only NA
## 5 5 Chidiegwu Dunkel Pizza Breakfast and lunch 5
## 6 6 Güvenç Attila Ice cream Lunch only 6