I am trying to form 7 groups of random number of observations from a total of 100 observations. All observations should be used

瘦欲@ 提交于 2020-01-06 06:46:27

问题


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!


回答1:


Try something like this:

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

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


来源:https://stackoverflow.com/questions/57240278/i-am-trying-to-form-7-groups-of-random-number-of-observations-from-a-total-of-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!