[英]A list of n choose k+1 combinations of observations from a n x k data frame in R
Suppose I have a data frame with n rows and k columns.假设我有一个包含 n 行和 k 列的数据框。 I would like to have a list that contains unique n choose k+1 data frames.
我想要一个包含唯一 n 选择 k+1 数据帧的列表。 These data frames then has size k + 1 rows and k columns.
然后这些数据帧的大小为 k + 1 行和 k 列。 How do I do this in R?
如何在 R 中执行此操作? I know
我知道
combn()
but it works on vector, not matrix.但它适用于向量,而不是矩阵。
If your data frame is called df
you could do:如果您的数据框被称为
df
你可以这样做:
apply(combn(nrow(df), ncol(df) + 1), 2, function(i) df[i,])
For example:例如:
df <- data.frame(x = 1:4, y = c('A', 'B', 'C', 'D'))
apply(combn(nrow(df), ncol(df) + 1), 2, function(i) df[i,])
# [[1]]
# x y
# 1 1 A
# 2 2 B
# 3 3 C
#
# [[2]]
# x y
# 1 1 A
# 2 2 B
# 4 4 D
#
# [[3]]
# x y
# 1 1 A
# 3 3 C
# 4 4 D
#
# [[4]]
# x y
# 2 2 B
# 3 3 C
# 4 4 D
Just be aware that you will very quickly run into memory problems if your data frame has more than a few columns and a modest number of rows.请注意,如果您的数据框具有多于几列和适度数量的行,您将很快遇到 memory 问题。 For example, a data frame with just 4 columns and 50 rows will generate a list of over two million data frames here, and that will increase to 75 million with 100 rows.
例如,一个只有 4 列和 50 行的数据框将在此处生成超过 200 万个数据框的列表,而这将增加到 100 行的 7500 万个数据框。 This is not a problem with the algorithm;
这不是算法的问题; it is just how many unique combinations there are.
它只是有多少独特的组合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.