r - R中的高效随机抽样

标签 r data.table tidyverse

从数据框中,我尝试随机抽样 1:20 的观察结果,其中 每个观察次数我想将过程复制 4 次。我 想出了这个可行的解决方案,但它很慢,因为它是 由于 crossing(),涉及多次处理大型数据帧 功能。任何人都可以指出我更有效的解决方案吗?

library(tidyverse)

mtcars %>% 
  group_by(cyl) %>% 
  nest() %>% 
  crossing(n_random_sample = 1:20, n_replicate = 1:4) %>% 
  mutate(res = map2_dbl(data, n_random_sample, function(data, n) {

    data %>%
      sample_n(n, replace = TRUE) %>%
      summarise(mean_mpg = mean(mpg)) %>%
      pull(mean_mpg)

  }))
#> # A tibble: 240 x 5
#>      cyl data              n_random_sample n_replicate   res
#>    <dbl> <list>                      <int>       <int> <dbl>
#>  1     6 <tibble [7 × 10]>               1           1  17.8
#>  2     6 <tibble [7 × 10]>               1           2  21  
#>  3     6 <tibble [7 × 10]>               1           3  19.2
#>  4     6 <tibble [7 × 10]>               1           4  18.1
#>  5     6 <tibble [7 × 10]>               2           1  19.6
#>  6     6 <tibble [7 × 10]>               2           2  19.4
#>  7     6 <tibble [7 × 10]>               2           3  19.6
#>  8     6 <tibble [7 × 10]>               2           4  20.4
#>  9     6 <tibble [7 × 10]>               3           1  20.1
#> 10     6 <tibble [7 × 10]>               3           2  18.9
#> # ... with 230 more rows

reprex package 创建于 2018-11-19 (v0.2.1)

编辑:我现在正在处理一个更大的数据集。是否可以使用 data.table 更有效地做到这一点?

最佳答案

这是一个替代解决方案,它对原始数据集进行子集化并使用函数选择行样本,而不是使用 nest 创建子数据集并将它们存储为列表变量,然后使用 map 选择一个示例:

library(tidyverse)

# create function to sample rows
f = function(c, n) {
  mtcars %>%
    filter(cyl == c) %>%
    sample_n(n, replace = TRUE) %>%
    summarise(mean_mpg = mean(mpg)) %>%
    pull(mean_mpg)
}

# vectorise function
f = Vectorize(f)

# set seed for reproducibility
set.seed(11)

tbl_df(mtcars) %>%
  distinct(cyl) %>%
  crossing(n_random_sample = 1:20, n_replicate = 1:4) %>%
  mutate(res = f(cyl, n_random_sample))

# # A tibble: 240 x 4
#     cyl n_random_sample n_replicate   res
#   <dbl>           <int>       <int> <dbl>
# 1     6               1           1  21  
# 2     6               1           2  21  
# 3     6               1           3  18.1
# 4     6               1           4  21  
# 5     6               2           1  20.4
# 6     6               2           2  21.2
# 7     6               2           3  20.4
# 8     6               2           4  19.6
# 9     6               3           1  18.4
#10     6               3           2  19.6
# # ... with 230 more rows

关于r - R中的高效随机抽样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53376574/

相关文章:

r - naiveBayes为什么会在R中返回所有NA以进行多类分类?

r - ggplot 图例中的虚线

r - 数据表与 dplyr : can one do something well the other can't or does poorly?

r - 对 r 中的动态列进行条件求和

r - 使用列索引而不是列名在 R dplyr 中进行变异

r - 将列值的一部分提取到新变量(变异)

r - 当列名是年份时减去列

r - 两个数据框之间的逐元素百分比变化

r - 如何从嵌套列表中正确省略 NA?

r - R左外连接以0填充代替NA,同时保留左表中的有效NA