[英]How to obtain all possible sub-samples of size n from a dataframe of size N in R?
I have a dataframe with 20 classrooms [1 to 20] indexes and 20 different number of students in each class, how to obtain all sub-samples of size n = 8 and store them because i want to use them later for calculations. 我有一个包含20个教室[1到20]索引的数据框和每个班级20个不同数量的学生,如何获得大小为n = 8的所有子样本并存储它们,因为我想稍后使用它们进行计算。 I used
combn()
but that takes only one vector, can i use it with a dataframe and how? 我使用了
combn()
但只需要一个向量,我可以将它与数据帧一起使用吗? (sorry but i'm new in R), dataframe below: (抱歉,但我是R的新手),数据框如下:
classrooms students 1 1 29 2 2 30 3 3 35 4 4 28 5 5 32 6 6 20 7 7 25 8 8 22 9 9 32 10 10 26 11 11 27 12 12 34 13 13 27 14 14 28 15 15 33 16 16 21 17 17 36 18 18 24 19 19 19 20 20 32
It is as simple as passing a function to combn
. 它就像将函数传递给
combn
一样简单。 simplify = FALSE
means that a list will be returned. simplify = FALSE
表示将返回一个列表。
Assuming you want all possible combinations of 8 classrooms from the dataset classrooms 假设您想要数据集教室中8个教室的所有可能组合
combinations <- combn(nrow(classrooms), 8, function(x,data) data[x,],
simplify = FALSE, data =classrooms )
head(combinations, n = 2)
[[1]]
classrooms students
1 1 29
2 2 30
3 3 35
4 4 28
5 5 32
6 6 20
7 7 25
8 8 22
[[2]]
classrooms students
1 1 29
2 2 30
3 3 35
4 4 28
5 5 32
6 6 20
7 7 25
9 9 32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.