使用 rbind 反转应用

发布于 2024-12-14 10:11:26 字数 411 浏览 0 评论 0原文

假设我在 R 中有一个数组 foo,其维度 == c(150, 40, 30)。 现在,如果我:

bar <- apply(foo, 3, rbind)

dim(bar) 现在是 c(6000, 30)

反转此过程并从 barfoo 以使它们相同的最优雅和通用的方法是什么?

问题不在于获得正确的维度,而在于在其受尊重的原始维度内以相同的顺序获取数据。

感谢您抽出时间,期待您的回复。

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

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

发布评论

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

评论(2

眼前雾蒙蒙 2024-12-21 10:11:26

我认为你可以再次调用 array 并指定原始尺寸:

m <- array(1:210,dim = c(5,6,7))
m1 <- apply(m, 3, rbind)
m2 <- array(as.vector(m1),dim = c(5,6,7))
all.equal(m,m2)
[1] TRUE

I think you can just call array again and specify the original dimensions:

m <- array(1:210,dim = c(5,6,7))
m1 <- apply(m, 3, rbind)
m2 <- array(as.vector(m1),dim = c(5,6,7))
all.equal(m,m2)
[1] TRUE
自找没趣 2024-12-21 10:11:26

我想知道你最初的转变。您从 apply 调用 rbind,但这不会执行任何操作 - 您也可以调用 identity

foo <- array(seq(150*40*30), c(150, 40, 30))
bar <- apply(foo, 3, rbind)
bar2 <- apply(foo, 3, identity)
identical(bar, bar2) # TRUE

那么,您真正想要实现的目标是什么?我假设您有几个(30)个矩阵切片,并且想要将它们堆叠起来,然后再次将它们拆开。如果是这样,代码将比 @joran 建议的更复杂。您需要对 aperm 进行一些调用(如@Patrick Burns 建议的那样):

# Make a sample 3 dimensional array (two 4x3 matrix slices):
m <- array(1:24, 4:2)

# Stack the matrix slices on top of each other
m2 <- matrix(aperm(m, c(1,3,2)), ncol=ncol(m))

# Reverse the process
m3 <- aperm(array(m2, c(nrow(m),dim(m)[[3]],ncol(m))), c(1,3,2))

identical(m3,m) # TRUE

无论如何,aperm 确实很强大(但有些令人困惑)。非常值得学习...

I'm wondering about your initial transformation. You call rbind from apply, but that won't do anything - you could just as well have called identity!

foo <- array(seq(150*40*30), c(150, 40, 30))
bar <- apply(foo, 3, rbind)
bar2 <- apply(foo, 3, identity)
identical(bar, bar2) # TRUE

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):

# Make a sample 3 dimensional array (two 4x3 matrix slices):
m <- array(1:24, 4:2)

# Stack the matrix slices on top of each other
m2 <- matrix(aperm(m, c(1,3,2)), ncol=ncol(m))

# Reverse the process
m3 <- aperm(array(m2, c(nrow(m),dim(m)[[3]],ncol(m))), c(1,3,2))

identical(m3,m) # TRUE

In any case, aperm is really powerful (and somewhat confusing). Well worth learning...

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