我试图从总共100个观察中形成7组随机观察.应该使用所有观察结果 [英] I am trying to form 7 groups of random number of observations from a total of 100 observations. All observations should be used

查看:98
本文介绍了我试图从总共100个观察中形成7组随机观察.应该使用所有观察结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含100个观察值的7组列表.每个组可以具有不同数量的观察值.所有观察值应置于7组之一中.换句话说,应使用所有观察结果.

I am trying to create a list of 7 groups with 100 observations. Each group can have different number of observations. All observations should be placed in one of the 7 groups. In other words, all observations should be used.

我使用的代码并未使用所有观察结果.有什么办法可以解决这个问题?

The code I am using does not use all the observations. Is there a way that I can solve this?

times_to_sample = 7L
  NN = nrow(df)
  sample<-replicate(times_to_sample, df[sample(NN, sample(5:15, 1L)), ], simplify = FALSE)

我的预期结果只需要将每个观察值放在七个组之一中即可.任何帮助将不胜感激.谢谢!

my expected result just has to place each observation in one of the seven groups. Any help will be appreciated. Thank you!

推荐答案

尝试如下操作:

group_indices <- sample(x = 1:7, size = 100, replace = TRUE)

df_splitted_in_7_groups <- split(x = df, f = group_indices)

这篇关于我试图从总共100个观察中形成7组随机观察.应该使用所有观察结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆