随机采样100次,将数据集放入列表中

发布于 2025-02-08 21:34:03 字数 421 浏览 2 评论 0原文

我有一个带有多个ID的数据集,看起来像这样(缩短版本):

  ID        Value
  bear 1      1
  bear 1      2
  bear 1      5
  bear 2      2
  bear 2      3
  bear 2      1
  bear 2      1
  bear 2      4

我想每个ID号进行采样一行,然后将这些行分为数据框架,例如:

  ID        Value

  bear 1      2
  bear 2      1

我想将此随机采样100次,取结果100个数据集,并将其作为列表中的元素。

关于如何这样做的任何想法吗?使用某些apply()/lapply()/sapply()函数或循环?

I have a dataset with multiple ID's that looks like this (shortened version):

  ID        Value
  bear 1      1
  bear 1      2
  bear 1      5
  bear 2      2
  bear 2      3
  bear 2      1
  bear 2      1
  bear 2      4

I would like to sample one row per ID number and make those rows into a data frame, like this for example:

  ID        Value

  bear 1      2
  bear 2      1

I then want to this random sampling 100 times, take the resulting 100 datasets and make them elements in a list.

Any thoughts on how to do this? Either using some of the apply()/lapply()/sapply() functions or a loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扶醉桌前 2025-02-15 21:34:03

我们可以使用rerunslice_sample - 重新运行复制采样的输出n times,然后返回list list < /代码>

library(purrr)
library(dplyr)
n <- 100
rerun(n, df1 %>%
             group_by(ID) %>% 
             slice_sample(n = 1) %>%
             ungroup)

We may use rerun with slice_sample- rerun replicates the sampled output n times and return a list

library(purrr)
library(dplyr)
n <- 100
rerun(n, df1 %>%
             group_by(ID) %>% 
             slice_sample(n = 1) %>%
             ungroup)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文