我有一个带有50行的df。我想模拟它,就好像只有5行,但是我想做很多次。我知道我可以单独执行此操作并将它们绑定在一起,但这是愚蠢的。希望有一种方法可以执行一个命令。我在想某种map()
或do{}
,但目前我不太熟悉。
如果可能,我想保持tidyverse语法,因为%>%
管道将继续。
# make df
df <- data.frame(sample=c(1:50),
y = sample(1:100, 50, replace=T) )
# sample df
df %>%
sample_n(5) %>%
mutate(simulation = 1)
# so this does what I want once, but I want to repeat this, say, 20 times.
# desired output:
sample y simulation
1 34 4 1
2 7 1 1
3 26 59 1
4 41 56 1
5 44 82 1
6 25 9 2
7 40 11 2
8 13 39 2
9 6 56 2
10 24 80 3
11 36 41 3
12 9 2 3
13 11 56 3
14 1 36 3
15 25 8 3
# and so on to 20 simulations.
谢谢!
replicate(..., simplify=FALSE)
将执行您需要的操作快速,然后您可以在以后进行绑定。
library(dplyr)
bind_rows(replicate(3, sample_n(df, 5), simplify = FALSE),
.id = "simulation")
# simulation sample y
# 1 1 12 3
# 2 1 42 22
# 3 1 14 50
# 4 1 3 6
# 5 1 45 46
# 6 2 2 9
# 7 2 30 18
# 8 2 24 66
# 9 2 50 62
# 10 2 21 96
# 11 3 3 6
# 12 3 33 47
# 13 3 25 12
# 14 3 32 96
# 15 3 20 4
或完全在tidyverse中:
purrr::map_dfr(1:3, ~ sample_n(df, 5), .id = "simulation")
这样的东西
library(tidymodels)
#> -- Attaching packages ---------------------------------------------------------- tidymodels 0.1.0 --
#> v broom 0.5.6 v recipes 0.1.12
#> v dials 0.0.6 v rsample 0.0.7
#> v dplyr 1.0.0 v tibble 3.0.1
#> v ggplot2 3.3.1 v tune 0.1.0
#> v infer 0.5.1 v workflows 0.1.1
#> v parsnip 0.1.1 v yardstick 0.0.6
#> v purrr 0.3.4
#> -- Conflicts ------------------------------------------------------------- tidymodels_conflicts() --
#> x purrr::discard() masks scales::discard()
#> x dplyr::filter() masks stats::filter()
#> x dplyr::lag() masks stats::lag()
#> x ggplot2::margin() masks dials::margin()
#> x recipes::step() masks stats::step()
df <- data.frame(sample=c(1:50),
y = sample(1:100, 50, replace=T) )
df %>%
specify(formula = y ~ sample) %>%
hypothesize(null = "independence") %>%
generate(reps = 100,type = "permute") %>%
slice_head(n = 5)
#> # A tibble: 500 x 3
#> # Groups: replicate [100]
#> y sample replicate
#> <int> <int> <int>
#> 1 63 1 1
#> 2 53 2 1
#> 3 57 3 1
#> 4 44 4 1
#> 5 82 5 1
#> 6 92 1 2
#> 7 83 2 2
#> 8 89 3 2
#> 9 17 4 2
#> 10 35 5 2
#> # ... with 490 more rows
由reprex package(v0.3.0)在2020-06-16创建