Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Created July 6, 2022 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpghayes/a2a436a24560edf3307267eaffb38500 to your computer and use it in GitHub Desktop.
Save alexpghayes/a2a436a24560edf3307267eaffb38500 to your computer and use it in GitHub Desktop.
``` r
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
d <- tibble(
GENDER = sample(1:3, 10, replace = TRUE)
)
d |>
mutate(
gender_chr = case_when(
GENDER == 1 ~ "male",
GENDER == 2 ~ "female",
TRUE ~ "other"
),
gender_fct = as.factor(gender_chr)
)
#> # A tibble: 10 × 3
#> GENDER gender_chr gender_fct
#> <int> <chr> <fct>
#> 1 1 male male
#> 2 3 other other
#> 3 3 other other
#> 4 2 female female
#> 5 2 female female
#> 6 3 other other
#> 7 1 male male
#> 8 1 male male
#> 9 3 other other
#> 10 1 male male
```
<sup>Created on 2022-07-06 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1.9000)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment