r - 表中行的成对组合?

发布于 2024-10-02 09:11:30 字数 632 浏览 6 评论 0原文

假设如下表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里的微风 2024-10-09 09:11:30

没有必要像这样将矩阵的行相乘,如果你有一个很大的数据集,可能会出现问题。相反,只需为每个实例挑选相关行即可。但预先创建选择很方便,可能是这样的:

xselect <- combn(1:nrow(X),2)

用您的数据进行说明(假设您只使用第 2 列和第 3 列):

X <- matrix(c("A", "B", "C", 0,2,1,1,NA,2),3,3)
Y <- rnorm(2, 4, 2)

for (i in 1:ncol(xselect))
{
  x1 <- as.numeric(X[xselect[1,i], c(2,3)])
  x2 <- as.numeric(X[xselect[2,i], c(2,3)])
  print(lm(Y ~ x1 * x2))
}

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:

xselect <- combn(1:nrow(X),2)

To illustrate with your data (assuming you only use columns 2 and 3):

X <- matrix(c("A", "B", "C", 0,2,1,1,NA,2),3,3)
Y <- rnorm(2, 4, 2)

for (i in 1:ncol(xselect))
{
  x1 <- as.numeric(X[xselect[1,i], c(2,3)])
  x2 <- as.numeric(X[xselect[2,i], c(2,3)])
  print(lm(Y ~ x1 * x2))
}
余生共白头 2024-10-09 09:11:30

我不确定您到底想对线性模型做什么,但要迭代 X(一次一对行),为每对生成一个因子,然后使用 < code>by

fac <- as.factor(sort(rep(1:(nrow(X)/2), 2)))
by(X, fac, FUN)

其中 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 use by

fac <- as.factor(sort(rep(1:(nrow(X)/2), 2)))
by(X, fac, FUN)

where FUN is whatever function you want to apply over the pairs of rows in X.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文