Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created September 18, 2020 04:02
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 njtierney/2388972c7078062682906ae92352ab48 to your computer and use it in GitHub Desktop.
Save njtierney/2388972c7078062682906ae92352ab48 to your computer and use it in GitHub Desktop.

some things on purrr

# using bind_rows
head(bind_rows(one = cars, 
               two = cars, 
               .id = "anything"))
#> Error in bind_rows(one = cars, two = cars, .id = "anything"): could not find function "bind_rows"
tail(bind_rows(one = cars, two = cars, .id = "anything"))
#> Error in bind_rows(one = cars, two = cars, .id = "anything"): could not find function "bind_rows"

purrr::map2_df(.x = list(one = 1, two = 50, three = 100), 
                .y = list(one = 10,two =  2, three = 1),
                .f = ~rnorm(n = 10, 
                            mean = .x,
                            sd = .y))
#> # A tibble: 10 x 3
#>       one   two three
#>     <dbl> <dbl> <dbl>
#>  1  -5.56  48.1 101. 
#>  2 -15.6   51.8  97.5
#>  3  -3.68  48.4  99.5
#>  4   5.89  46.1 101. 
#>  5  -6.91  49.6 100. 
#>  6  -8.52  50.8 100. 
#>  7  -3.57  51.6  99.8
#>  8   6.91  45.0 100. 
#>  9 -25.1   49.5 101. 
#> 10  12.3   50.0 100.

purrr::pmap(.l = list(mean = list(1, 50, 100), 
                      sd = list(10, 2, 1), 
                      n = list(1,2,10)),
            .f = rnorm)
#> [[1]]
#> [1] 13.48273
#> 
#> [[2]]
#> [1] 50.45065 50.71595
#> 
#> [[3]]
#>  [1]  99.40228 100.09047 101.49171  98.55780 100.61768  98.88516 102.12961
#>  [8] 100.78080  98.97738  99.54191

Created on 2020-09-18 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment