将2x6阵列重新整形为3x2x2,逐行

时间:2018-05-27 00:10:46

标签: arrays r multidimensional-array reshape

我有以下数组:

set.seed(10)
data <- array(rexp(12), dim=c(2,6))

         [,1]      [,2]      [,3]       [,4]      [,5]      [,6]
[1,] 1.398107 0.1704216 0.1664439 0.01057119 2.3546118 0.5222435
[2,] 1.110459 1.9187813 0.9702900 2.79300971 0.6672384 0.1467129

我想重新塑造它,以便得到一个[3,2,2]维度的数组,但是这样做是这样的:

, , 1

          [,1]      [,2]
[1,] 1.398107  0.1704216
[2,] 0.1664439 0.01057119
[3,] 2.3546118 05222435

, , 2

           [,1]      [,2]
[1,] 1.110459   1.9187813
[2,] 0.9702900  2.79300971
[3,] 0.6672386  0.1467129

我试过了array(data, dim=c(3,2,2)),但它没有按照我想要的方式做任何想法吗?

2 个答案:

答案 0 :(得分:2)

编辑: 更好的方法是class TestClassA: UIViewController { override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) // To improve this code, you'd pull out the Notification name and perhaps put it into an extension, instead of hardcoding it here and elsewhere. NotificationCenter.default.post(Notification.init(name: Notification.Name.init(rawValue: "viewControllerAppeared"))) } } class TestClassB: UIViewController { override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) NotificationCenter.default.addObserver(self, selector: #selector(viewControllerAppeared(notification:)), name: Notification.Name.init(rawValue: "viewControllerAppeared"), object: nil) } @objc func viewControllerAppeared(notification: NSNotification) { print("other viewcontroller appeared") } } 。感谢@smci的链接。

原始解决方案:

我使用该种子获得不同的数据,但您可以使用aperm(array(data, c(2, 2, 3)))matrix使每一行成为一个双列矩阵,然后lapply绑定到一个数组中。

abind

带管道

set.seed(10)
data <- array(rexp(12), dim=c(2,6))
data
# 
# [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
# [1,] 0.01495641 0.7521589 0.2316586 2.3276229 1.2883101 0.4265298
# [2,] 0.92022120 1.5750419 1.0866730 0.7291238 0.6722683 1.1154219

library(abind)
abind(lapply(split(data, row(data)), matrix, ncol = 2, byrow = T)
      , along = 3)

# , , 1
# 
# [,1]      [,2]
# [1,] 0.01495641 0.7521589
# [2,] 0.23165862 2.3276229
# [3,] 1.28831010 0.4265298
# 
# , , 2
# 
# [,1]      [,2]
# [1,] 0.9202212 1.5750419
# [2,] 1.0866730 0.7291238
# [3,] 0.6722683 1.1154219 

这里的选项要快得多,不需要包裹。但我并不认为它非常清楚。不知道怎么写得更清楚。

library(magrittr)
data %>% 
  split(row(.)) %>% 
  lapply(matrix, ncol = 2, byrow = T) %>% 
  abind(along = 3)

也许这个?

array(unlist(data)[order(rep(c(1, 3, 2, 4), 3))], dim = c(3, 2, 2))

答案 1 :(得分:1)

这可能是更快更简单的基础解决方案:

 array( aperm(data, c(2,1) ), c(3,2,2) )

我为该随机种子得到了一个不同的数组:

data
           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
[1,] 0.01495641 0.7521589 0.2316586 2.3276229 1.2883101 0.4265298
[2,] 0.92022120 1.5750419 1.0866730 0.7291238 0.6722683 1.1154219

aperm函数可以交换列的行:

> aperm(data, c(2,1))
           [,1]      [,2]
[1,] 0.01495641 0.9202212
[2,] 0.75215894 1.5750419
[3,] 0.23165862 1.0866730
[4,] 2.32762287 0.7291238
[5,] 1.28831010 0.6722683
[6,] 0.42652979 1.1154219

然后array可以重新排列到所需的结构:

>  array( aperm(data, c(2,1)), c(3,2,2))
, , 1

           [,1]      [,2]
[1,] 0.01495641 2.3276229
[2,] 0.75215894 1.2883101
[3,] 0.23165862 0.4265298

, , 2

          [,1]      [,2]
[1,] 0.9202212 0.7291238
[2,] 1.5750419 0.6722683
[3,] 1.0866730 1.1154219