in R, I am trying to create one column called model where the value depends on the value of up to 5 other columns.
The columns are Sport, Car, SUV, Wagon, Minivan, Pickup. These 5 columns have a value of 1 if it is a member of that group otherwise it has a 0. I would like to create one column called model that would equal 1 if Sport = 1,2 if SUV = 1, 3 if Wagon =1, 4 if Minivan = 1 , 5 if Pickup = 1 otherwise if all 5 columns equal 0 then model would = 0. I tried it using case statements and mutate using dplyr
cars %>%
mutate(model == case_when(Sport == 1, ~ '1'
,case_when(SUV == 1, ~ '2'
,case_when(Wagon == 1, ~'3'
,case_when(Minivan == 1, ~'4'
,case_when(Pickup == 1, ~ '5'
,TRUE ~ 0))))))
but I got this error:
> error: Problem with `mutate()` input `..1`.
i `..1 = ==...`.
x Case 1 (`Pickup == 1`) must be a two-sided formula, not a logical vector.
I have included a link to the file below. [04cars.csv file]
CodePudding user response:
You don't nest case_when
statements, You just pass all your expressions to one call to case_when
cars %>%
mutate(model = case_when(
Sport == 1 ~ '1',
SUV == 1 ~ '2',
Wagon == 1 ~'3',
Minivan == 1 ~'4',
Pickup == 1 ~ '5',
TRUE ~ 0))
One the first matching condition for each value will be used.