dplyr:样本数量大于总体数量

编程入门 行业动态 更新时间:2024-10-20 05:47:47
本文介绍了dplyr:样本数量大于总体数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个数据框:

> class(dataset) [1] "grouped_df" "tbl_df" "tbl" "data.frame" > dim(dataset) [1] 64480 39

我要从中采样50.000个样本

where I want to sample 50.000 samples from

> dataset %>% dplyr::sample_n(50000)

但总是给我错误

错误:样本大小(50000)大于总体大小(1)。您要替换= TRUE吗?

Error: Sample size (50000) greater than population size (1). Do you want to replace = TRUE?

但是例如有效的方法:

> dim(dataset[1] %>% dplyr::sample_n(50000)) [1] 50000 1

那为什么我的人口规模(1)-与分组有关吗?

So why is my population size (1) - does that have something to do with grouping?

推荐答案

是的,可能与分组有关。从 class(dataset)的输出中可以看到,您的数据当前已分组(注意 grouped_df 信息),并且显然,一个或多个组的观测值太少,无法对50000个观测值进行采样而不进行替换。

Yes, it probably has to do with grouping. As you can see from the output of class(dataset) your data is currently grouped (note the grouped_df info) and one or more groups apparently have too few observations to sample 50000 observations without replacement.

要解决此问题,您可以在采样前取消数据分组:

To resolve this, you can either ungroup your data before sampling:

dataset %>% ungroup() %>% sample_n(50000)

或者您可以带有替换的样本:

Or you can sample with replacement:

dataset %>% sample_n(50000, replace = TRUE)

更多推荐

dplyr:样本数量大于总体数量

本文发布于:2023-11-23 20:47:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数量   样本   总体   dplyr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!