0
starwars_ %$% table(sex, gender)

and now I have to use the function case_when() to recode the category “none” of sex into “male” if gender is “masculine” and into “female” if gender is “feminine”. Make sure that the categories of sex other than “none” are not converted to NA by specifying the TRUE ~ ... argument. Does anyone know how to do this?

1
  • 1
    Please provide enough code so others can better understand or reproduce the problem. Commented Mar 16, 2022 at 20:18

1 Answer 1

1

I think you want something like this:

library(dplyr)
library(magrittr)

starwars %>% 
  mutate(sex = case_when(gender == "feminine" ~ "female",
                         gender == "masculine" ~ "male",
                         TRUE ~ sex)) %$% 
  table(sex, gender)
#>         gender
#> sex      feminine masculine
#>   female       17         0
#>   male          0        66

Created on 2022-03-16 by the reprex package (v2.0.1)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.