Last active
February 12, 2020 17:43
-
-
Save avallecam/d493bc6a4e00be6d9891d70f5587491f to your computer and use it in GitHub Desktop.
R: resumir respuestas de opción múltiple
This file contains 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
``` r | |
library(tidyverse) | |
library(naniar) | |
# more on naniar: https://cran.r-project.org/web/packages/naniar/vignettes/replace-with-na.html | |
#create foo dataset | |
foo <- tibble(xA=c(1,rep(NA,2),2), | |
xB=c(rep(NA,2),5:4), | |
xC=2:5) %>% | |
mutate_all(.funs = as.double) | |
#execute coalesce, str_c and paste | |
foo %>% | |
mutate(coalesce_l = coalesce(!!! select(.,1:3)), | |
coalesce_r = coalesce(!!! select(.,3:1)), | |
str_c = str_c(!!! select(.,1:3),sep = ".")) %>% | |
mutate_at(.vars = vars(starts_with("x")), | |
.funs = str_replace_na, "") %>% | |
mutate(paste = paste(!!! select(.,1:3),sep=".")) %>% | |
replace_with_na_at(.vars = vars(starts_with("x")), | |
condition = ~.x %in% c("") ) | |
#> # A tibble: 4 x 7 | |
#> xA xB xC coalesce_l coalesce_r str_c paste | |
#> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> | |
#> 1 1 <NA> 2 1 2 <NA> 1..2 | |
#> 2 <NA> <NA> 3 3 3 <NA> ..3 | |
#> 3 <NA> 5 4 5 4 <NA> .5.4 | |
#> 4 2 4 5 2 5 2.4.5 2.4.5 | |
``` | |
<sup>Created on 2020-02-12 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment