【问题标题】:subsample a df multiple times in a pipe R在管道 R 中多次对 df 进行二次采样
【发布时间】: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包吗?

标签: r dplyr subset tidyverse


【解决方案1】:

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")

【讨论】:

  • 谢谢-我喜欢这个版本,但我在整个整洁的版本中遇到了一些问题,因为我也需要它来输入。就像,` df %>% map_dfr(1:2, ~ sample_n( . , 5), .id="simulation") ` 除非那不起作用。有什么想法吗?
  • 是的:df %&gt;% replicate(3, ., simplify = FALSE) %&gt;% map_dfr(~ sample_n(., 5), .id = "simulation")
【解决方案2】:

类似的东西

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 年 6 月 16 日创建

【讨论】:

  • 我认为补充一下排列不应该用于测试独立性以外的任何东西是很酷的