【发布时间】:2025-12-11 13:15:02
【问题描述】:
我有一个 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.
谢谢!
【问题讨论】:
-
有使用管道进行模拟分析的包你试过infer包吗?