Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
810 views
in Technique[技术] by (71.8m points)

r - Create new variable by multiple conditions via mutate case_when

Hi want to create a new variable/column (WHRcat) by 2 variables (WHR and sexe) under a certain condition wth dyplr, mutate and case_when.

Data:

WHR   sexe  WHRcat (new variable)
1.5    1
2.8    2
0.2    2
0.3    1
1.1    1

My code:

test<- test%>% mutate(WHRcat = case_when((WHR >= 1.02 & sexe = 1) ~ 1,
                                         (WHR < 1.02 & sexe = 1) ~ 2,
                                         (WHR >= 0.85 & sexe = 2) ~ 3,
                                         (WHR < 0.85 & sexe = 2) ~ 4,
                                          TRUE ~ 0)) 

Though doesnt work.

Error:

> test<- test%>% mutate(WHRcat = case_when((WHR >= 1.02 & sexe = 1) ~ 1,
+                      (WHR < 1.02 & sexe = 1) ~ 2,
+                      (WHR >= 0.85 & sexe = 2) ~ 3,
+                      (WHR < 0.85 & sexe = 2) ~ 4,
+                       TRUE ~ 0))
Error in WHR >= 1.02 & sexe = 1 : could not find function "&<-"

What am I doing wrong?

See this example which sould work:

#' # case_when is particularly useful inside mutate when you want to
#' # create a new variable that relies on a complex combination of existing
#' # variables
#' starwars %>%
#'   select(name:mass, gender, species) %>%
#'   mutate(
#'     type = case_when(
#'       height > 200 | mass > 200 ~ "large",
#'       species == "Droid"        ~ "robot",
#'       TRUE                      ~ "other"
#'     )
#'   )

from https://github.com/tidyverse/dplyr/blob/master/R/case_when.R

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The issue is in the use of assignment operator = instead of comparison ==

library(dplyr)
test<- test%>% 
       mutate(WHRcat = case_when((WHR >= 1.02 & sexe == 1) ~ 1,
                                         (WHR < 1.02 & sexe == 1) ~ 2,
                                         (WHR >= 0.85 & sexe == 2) ~ 3,
                                         (WHR < 0.85 & sexe == 2) ~ 4,
                                          TRUE ~ 0)) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...