Reputation: 7394
I want to simulate some coin flips of a fair and unfair coin using purrr. I have parameters for rbinom
as columns in a dataframe. Here is some code I've started with:
library(tidyverse)
#setting up params for rbinom
params = list(size = 600, n = 1, p_fair = 0.5,p_unfair = 0.6)
params %>%
purrr::map_df(~rep(., times = 10))
How can I use purrr
to pass different parameters p_fair
/p_unfair
into rbinom
and put the results as a column in my dataframe? I would like the result to look like:
size n p_fair p_unfair fair unfair
<dbl> <dbl> <dbl> <dbl> <int> <int>
1 600 1 0.5 0.6 308 373
2 600 1 0.5 0.6 305 367
3 600 1 0.5 0.6 280 367
4 600 1 0.5 0.6 299 374
5 600 1 0.5 0.6 298 360
6 600 1 0.5 0.6 298 346
7 600 1 0.5 0.6 301 359
8 600 1 0.5 0.6 292 376
9 600 1 0.5 0.6 300 347
10 600 1 0.5 0.6 305 357
Upvotes: 1
Views: 1300
Reputation: 3321
The straightforward approach isn't as elegant as some other answers, but more readable (and not much longer).
# Libraries
library(tidyverse)
# Data
params = list(size = 600, n = 1, p_fair = 0.5,p_unfair = 0.6)
df1 <- params %>% map_df(~rep(., times = 10))
Here's a reminder of the function and it's arguments
# rbinom(n, size, prob)
Create fair/unfair dataframes that contain all arguments (each column header named for the relevant argument).
p_fair_vars <- df1 %>%
select(n, size, p = p_fair)
p_unfair_vars <- df1 %>%
select(n, size, p = p_unfair)
Apply dataframes using pmap
.
df1 %>%
mutate(fair = pmap_int(p_fair_vars, rbinom),
unfair = pmap_int(p_unfair_vars, rbinom) )
#> # A tibble: 10 x 6
#> size n p_fair p_unfair fair unfair
#> <dbl> <dbl> <dbl> <dbl> <int> <int>
#> 1 600 1 0.5 0.6 296 351
#> 2 600 1 0.5 0.6 311 351
#> 3 600 1 0.5 0.6 290 356
#> 4 600 1 0.5 0.6 294 359
#> 5 600 1 0.5 0.6 294 348
#> 6 600 1 0.5 0.6 306 365
#> 7 600 1 0.5 0.6 288 370
#> 8 600 1 0.5 0.6 305 366
#> 9 600 1 0.5 0.6 314 327
#> 10 600 1 0.5 0.6 303 356
...or alternately:
df1 %>%
mutate(fair_vars = str_c(size, n, p_fair, sep="," ),
unfair_vars = str_c(size, n, p_unfair, sep = ","),
fair = pmap_int(p_fair_vars, rbinom),
unfair = pmap_int(p_unfair_vars, rbinom)) %>%
select(-c(5:6) )
Created on 2018-11-03 by the reprex package (v0.2.1)
Upvotes: 0
Reputation: 886938
We can loop through the 'p_' columns and apply the rbinom
library(dplyr)
library(purrr)
df1 %>%
select(matches('p_')) %>%
map(~ df1 %>%
select(n, size) %>%
mutate(p = .x) %>%
pmap_int(rbinom)) %>%
bind_cols %>%
rename_all(funs(sub("p_", "", .))) %>%
bind_cols(df1, .)
Another option would be to convert to 'long' format with gather
, apply the rbinom
and then reshape it back to 'wide'
library(tidyr)
df1 %>%
gather(key, p, p_fair:p_unfair) %>%
mutate(rval = pmap_int(.[c('n', 'size', 'p')], rbinom)) %>%
separate(key, into = c('key1', 'key2')) %>%
select(-key1) %>%
group_by(key2) %>%
mutate(n1 = row_number()) %>%
select(-p) %>%
spread(key2, rval) %>%
select(-n1, -n, -size) %>%
bind_cols(df1, .)
df1 <- params %>%
map_df(~rep(., times = 10))
Upvotes: 1