Last active
August 14, 2020 17:57
-
-
Save lcolladotor/01cbde66db960c1cde9a94ae746db4fb 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
| library("purrr") | |
| # View(mtcars) | |
| .x <- mtcars[[1]] | |
| mean(.x) | |
| map_dbl(mtcars, ~ mean(.x)) | |
| map_dbl(mtcars, mean) | |
| # rnorm() | |
| # args(rnorm) | |
| mean_values <- c(-10, 0, 10, 100) | |
| .x <- mean_values[[1]] | |
| rnorm(n = 10, mean = .x) | |
| map(mean_values, ~ rnorm(n = 10, mean = .x)) | |
| map_dfc(mean_values, ~ rnorm(10, mean = .x)) | |
| # remotes::install_github("allisonhorst/palmerpenguins") | |
| library("palmerpenguins") | |
| # data(package = 'palmerpenguins') | |
| .x <- penguins[[1]] | |
| length(unique(.x)) | |
| map_int(penguins, ~ length(unique(.x))) | |
| n_unique <- function(x) { | |
| length(unique(x)) | |
| } | |
| map_int(penguins, n_unique) | |
| ### 2020-08-14 | |
| library(ggplot2) | |
| # a list of data frames | |
| by_color <- split(diamonds, diamonds$color) | |
| # a vector of paths | |
| paths <- paste0(names(by_color), ".csv") | |
| library("purrr") | |
| map_dfr(by_color, class) | |
| class(paths) | |
| length(paths) | |
| length(by_color) | |
| # Solve for one | |
| .x <- by_color[[1]] | |
| .y <- paths[[1]] | |
| ?write.csv | |
| write.csv(x = .x, file = .y) | |
| map2(by_color, paths, ~ write.csv(x = .x, file = .y)) | |
| walk2(by_color, paths, ~ write.csv(x = .x, file = .y)) | |
| walk2(by_color, paths, write.csv) | |
| ## Won't work because the order of the inputs | |
| ## doesn't match the order of the function arguments | |
| walk2(paths, by_color, write.csv) | |
| ## You would need to use the full purrr syntax | |
| ## with the ~ | |
| walk2(paths, by_color, ~ write.csv(x = .y, file = .x)) | |
| ## | |
| df <- data.frame( | |
| a = 1L, | |
| b = 1.5, | |
| y = Sys.time(), | |
| z = ordered(1) | |
| ) | |
| df[1:4] %>% sapply(class) %>% str() | |
| df[1:2] %>% sapply(class) %>% str() | |
| df[3:4] %>% sapply(class) %>% str() | |
| df[1:4] %>% map_chr(class) %>% str() | |
| df[1:2] %>% map_chr(class) %>% str() | |
| df[3:4] %>% map_chr(class) %>% str() | |
| # | |
| # library(IRanges) | |
| # df[1:4] %>% map_chr(~ CharacterList(class(.x))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment