Created
October 16, 2020 08:24
-
-
Save lwaldron/e106c6cc114985c890d9c20a608d268a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I create and discuss this code at https://youtu.be/nU_GEPKVXU8 | |
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 | |
rawdat <- tibble(n = c("1", "2-4", "5-7")) | |
cleandat <- rawdat %>% | |
mutate(nmin = if_else(n == "2-4", "2", n)) %>% | |
mutate(nmax = if_else(n == "2-4", "4", n)) %>% | |
mutate(nmin = if_else(n == "5-7", "5", nmin)) %>% | |
mutate(nmax = if_else(n == "5-7", "7", nmax)) %>% | |
mutate(nmax = as.numeric(nmax)) %>% | |
mutate(nmin = as.numeric(nmin)) | |
cleandat2 <- rawdat %>% | |
mutate(nmin = as.numeric(recode(n, "2-4" = "2", "5-7" = "5"))) %>% | |
mutate(nmax = as.numeric(recode(n, "2-4" = "4", "5-7" = "7"))) | |
cleandat | |
#> # A tibble: 3 x 3 | |
#> n nmin nmax | |
#> <chr> <dbl> <dbl> | |
#> 1 1 1 1 | |
#> 2 2-4 2 4 | |
#> 3 5-7 5 7 | |
cleandat2 | |
#> # A tibble: 3 x 3 | |
#> n nmin nmax | |
#> <chr> <dbl> <dbl> | |
#> 1 1 1 1 | |
#> 2 2-4 2 4 | |
#> 3 5-7 5 7 | |
identical(cleandat, cleandat2) | |
#> [1] TRUE | |
all.equal(cleandat, cleandat2) | |
#> [1] TRUE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment