Home > Software design >  How to use purr to simulate data?
How to use purr to simulate data?

Time:12-18

I have a table looking more or less like this:

x y
1 5
2 7.5
3 12.7

I would now like to take every y and put it as mean in rnorm() with whatever var to generate, let's say, 100 values. I want to do that for every x. In the end, I would like to have a new table, basically the same as above but with the generated numbers in y and x showing if y belongs to 1, 2, or 3. I know this is possible with purrr, but I've never done it before.

I hope somebody can help.

Cheers

My actual data frame is way longer, so it wouldn't make much sense to do everything by hand and then join them together later.

CodePudding user response:

You can map() over y and then unnest:

library(tidyverse)

# original data
d <- tibble(x = c(1, 2, 3), y = c(5, 7.5, 12.7))

d |> 
  mutate(y = map(y, rnorm, n = 100)) |> 
  unnest(y)
#> # A tibble: 300 × 2
#>        x     y
#>    <dbl> <dbl>
#>  1     1  3.32
#>  2     1  3.78
#>  3     1  4.26
#>  4     1  4.03
#>  5     1  5.89
#>  6     1  5.31
#>  7     1  3.57
#>  8     1  2.64
#>  9     1  4.10
#> 10     1  5.60
#> # … with 290 more rows

Created on 2022-12-15 with reprex v2.0.2

CodePudding user response:

The answer above doesn't work; it seems like map() takes the first value and generates three datasets with it.

So this here works now for me:

d <- tibble(x = c(1, 2, 3), y = c(5, 7.5, 12.7)) %>%
mutate(y = map(y, ~rnorm(100, .x, 2))) %>% 
unnest(y)
  • Related