在 R 中创建所有数组索引的数据框
使用 R,我尝试构建给定矩阵的行数和列数的数据框。例如,如果
a <- matrix(c(1:15), nrow=5, ncol=3)
我想要构建一个数据框,给出:
row col
1 1
1 2
1 3
. .
5 1
5 2
5 3
我尝试过的:
row <- matrix(row(a), ncol=1, nrow=dim(a)[1]*dim(a)[2], byrow=T)
col <- matrix(col(a), ncol=1, nrow=dim(a)[1]*dim(a)[2], byrow=T)
out <- cbind(row, col)
colnames(out) <- c("row", "col")
结果:
row col
[1,] 1 1
[2,] 2 1
[3,] 3 1
[4,] 4 1
[5,] 5 1
[6,] 1 2
[7,] 2 2
[8,] 3 2
[9,] 4 2
[10,] 5 2
[11,] 1 3
[12,] 2 3
[13,] 3 3
[14,] 4 3
[15,] 5 3
这不是我想要的,因为行和列的顺序突然颠倒了,甚至我指定的很难“拜罗=T”。我不知道我是否以及在哪里犯了错误,但非常感谢克服这个问题的建议。提前致谢!
Using R, I'm trying to construct a dataframe of the row and col numbers of a given matrix. E.g., if
a <- matrix(c(1:15), nrow=5, ncol=3)
then I'm looking to construct a dataframe that gives:
row col
1 1
1 2
1 3
. .
5 1
5 2
5 3
What I've tried:
row <- matrix(row(a), ncol=1, nrow=dim(a)[1]*dim(a)[2], byrow=T)
col <- matrix(col(a), ncol=1, nrow=dim(a)[1]*dim(a)[2], byrow=T)
out <- cbind(row, col)
colnames(out) <- c("row", "col")
results in:
row col
[1,] 1 1
[2,] 2 1
[3,] 3 1
[4,] 4 1
[5,] 5 1
[6,] 1 2
[7,] 2 2
[8,] 3 2
[9,] 4 2
[10,] 5 2
[11,] 1 3
[12,] 2 3
[13,] 3 3
[14,] 4 3
[15,] 5 3
Which isn't what I'm looking for, as the sequence of rows and cols in suddenly reversed, even tough I specified "byrow=T". I don't see if and where I'm making a mistake but would hugely appreciate suggestions to overcome this problem. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将在向量 1:ncol 和 1:nrow 上使用 Expand.grid,然后用 [,2:1] 翻转列以按照您想要的顺序获取它们:
I'd use expand.grid on the vectors 1:ncol and 1:nrow, then flip the columns with [,2:1] to get them in the order you want:
使用
row
和col
,但更直接地操作它们的输出顺序,因为它们返回输入数组的相应索引。使用t
获得您最终想要的非默认顺序:或者,作为矩阵而不是 data.frame:
Use
row
andcol
, but more directly manipulate their output ordering since they return corresponding indices in place for the input array. Uset
to get the non-default order you want in the end:Or, as a matrix not a data.frame:
您可能想看看
?expand.grid
,它几乎完全符合您想要实现的目标。You may want to have a look at
?expand.grid
, which does just about exactly what you want to achieve.由于给猫剥皮的方法有很多种,我将根据
rep
提供另一种变体:...但要宣布“获胜者”,我认为您需要计算解决方案的时间:
...看来@Spacedman 拥有最快的解决方案!
Since there are many ways to skin a cat, I'll chip in with yet another variant based on
rep
:...but to announce a "winner", I think you need to time the solutions:
...so it seems @Spacedman has the speediest solution!