r - 表中行的成对组合?
假设如下表:
X =
col1 col2 col3
row1 "A" "0" "1"
row2 "B" "2" "NA"
row3 "C" "1" "2"
我使用下面的代码选择两行的组合:
pair <- apply(X, 2, combn, m=2)
这将返回以下形式的矩阵:
pair =
[,1] [,2] [,3]
[1,] "A" "0" "1"
[2,] "B" "2" NA
[3,] "A" "0" "1"
[4,] "C" "1" "2"
[5,] "B" "2" NA
[6,] "C" "1" "2"
我希望迭代对,一次获取两行,即首先隔离 [1,] 和 [2 ,],然后是 [3,] 和 [4,],最后是 [5,] 和 [6,]。然后,这些行将作为参数传递给回归模型,即 lm(Y ~ row[i]*row[j])。
我正在处理一个大数据集。有人能建议如何一次迭代矩阵两行,将这些行分配给变量并作为参数传递给函数吗?
谢谢, S;-)
Assume a table as below:
X =
col1 col2 col3
row1 "A" "0" "1"
row2 "B" "2" "NA"
row3 "C" "1" "2"
I select combinations of two rows, using the code below:
pair <- apply(X, 2, combn, m=2)
This returns a matrix of the form:
pair =
[,1] [,2] [,3]
[1,] "A" "0" "1"
[2,] "B" "2" NA
[3,] "A" "0" "1"
[4,] "C" "1" "2"
[5,] "B" "2" NA
[6,] "C" "1" "2"
I wish to iterate over pair, taking two rows at a time, i.e. first isolate [1,] and [2,], then [3,] and [4,] and finaly, [5,] and [6,]. These rows will then be passed as arguments to regression models, i.e. lm(Y ~ row[i]*row[j]).
I am dealing with a large dataset. Can anybody advise how to iterate over a matrix two rows at a time, assign those rows to variables and pass as arguments to a function?
Thanks,
S ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有必要像这样将矩阵的行相乘,如果你有一个很大的数据集,可能会出现问题。相反,只需为每个实例挑选相关行即可。但预先创建选择很方便,可能是这样的:
用您的数据进行说明(假设您只使用第 2 列和第 3 列):
It is unnecessary to multiply the rows of your matrix like that, and if you have a large data set it is might get problematic. In stead just pick out the relevant rows for each instance. But it is convenient to create the selection beforehand, something like this perhaps:
To illustrate with your data (assuming you only use columns 2 and 3):
我不确定您到底想对线性模型做什么,但要迭代
X
(一次一对行),为每对生成一个因子,然后使用 < code>by其中
FUN
是您想要对 X 中的行对应用的任何函数。I'm not sure exactly what you're trying to do with the linear models, but to iterate over
X
, a pair of rows at a time, make a factor for each pair, and then useby
where
FUN
is whatever function you want to apply over the pairs of rows in X.