使用 rbind 反转应用
假设我在 R 中有一个数组 foo
,其维度 == c(150, 40, 30)
。 现在,如果我:
bar <- apply(foo, 3, rbind)
dim(bar)
现在是 c(6000, 30)
。
反转此过程并从 bar
到 foo
以使它们相同的最优雅和通用的方法是什么?
问题不在于获得正确的维度,而在于在其受尊重的原始维度内以相同的顺序获取数据。
感谢您抽出时间,期待您的回复。
PS 对于那些认为这是一个更大问题的一部分的人来说,确实如此,不,我还不能使用 plyr
。
Lets say that I have an array, foo
, in R that has dimensions == c(150, 40, 30)
.
Now, if I:
bar <- apply(foo, 3, rbind)
dim(bar)
is now c(6000, 30)
.
What is the most elegant and generic way to invert this procedure and go from bar
to foo
so that they are identical?
The trouble isn't getting the dimensions right, but getting the data back in the same order, within it's respected, original, dimension.
Thank you for taking the time, I look forward to your responses.
P.S. For those thinking that this is part of a larger problem, it is, and no, I cannot use plyr
, quite yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你可以再次调用 array 并指定原始尺寸:
I think you can just call
array
again and specify the original dimensions:我想知道你最初的转变。您从
apply
调用rbind
,但这不会执行任何操作 - 您也可以调用identity
!那么,您真正想要实现的目标是什么?我假设您有几个(30)个矩阵切片,并且想要将它们堆叠起来,然后再次将它们拆开。如果是这样,代码将比 @joran 建议的更复杂。您需要对
aperm
进行一些调用(如@Patrick Burns 建议的那样):无论如何,
aperm
确实很强大(但有些令人困惑)。非常值得学习...I'm wondering about your initial transformation. You call
rbind
fromapply
, but that won't do anything - you could just as well have calledidentity
!So, what is it you really wanted to accomplish? I was under the assumption that you had a couple (30) matrix slices and wanted to stack them and then unstack them again. If so, the code would be more involved than @joran suggested. You need some calls to
aperm
(as @Patrick Burns suggested):In any case,
aperm
is really powerful (and somewhat confusing). Well worth learning...