如何在不使用 for 循环的情况下重塑 R 基矩阵?

发布于 2025-01-18 12:18:27 字数 211 浏览 0 评论 0原文

我有一些具有以下维度的数据 [100,2,100]。它代表 100 行观察结果、2 列变量,在 100 次实验中重复。

我希望将数据放在维度为 [10000, 2] 的矩阵中,其中我已经有效地消除了实验的概念。

类似于 python 中的 .reshape 。

我找到了重塑包。不过我想知道如果没有它是否可以完成。 在基本 R 中是否有比 for 循环更优雅的方法?

I have some data that has the following dimension
[100, 2, 100]. It represents 100 rows of observations, 2 columns of variables, repeated in 100 experiments.

I would like to have the data in a matrix with the dimensions [10000, 2], where I have effectively eliminated the concept of experiments.

Something like .reshape in python.

I found the reshape package. However I wonder if it can be done without it.
Is there a more elegant way to do this in base R than a for loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

东风软 2025-01-25 12:18:27

让我们从一个较小的例子开始,数组 m

m <- array(rep(1:100, 5), c(10, 2, 5))

是一个 3-D 数组。

> m
, , 1

      [,1] [,2]
 [1,]    1   11
 [2,]    2   12
 [3,]    3   13
 [4,]    4   14
 [5,]    5   15
 [6,]    6   16
 [7,]    7   17
 [8,]    8   18
 [9,]    9   19
[10,]   10   20

, , 2

      [,1] [,2]
 [1,]   21   31
 [2,]   22   32
 [3,]   23   33
 [4,]   24   34
 [5,]   25   35
 [6,]   26   36
 [7,]   27   37
 [8,]   28   38
 [9,]   29   39
[10,]   30   40

, , 3

      [,1] [,2]
 [1,]   41   51
 [2,]   42   52
 [3,]   43   53
 [4,]   44   54
 [5,]   45   55
 [6,]   46   56
 [7,]   47   57
 [8,]   48   58
 [9,]   49   59
[10,]   50   60

, , 4

      [,1] [,2]
 [1,]   61   71
 [2,]   62   72
 [3,]   63   73
 [4,]   64   74
 [5,]   65   75
 [6,]   66   76
 [7,]   67   77
 [8,]   68   78
 [9,]   69   79
[10,]   70   80

, , 5

      [,1] [,2]
 [1,]   81   91
 [2,]   82   92
 [3,]   83   93
 [4,]   84   94
 [5,]   85   95
 [6,]   86   96
 [7,]   87   97
 [8,]   88   98
 [9,]   89   99
[10,]   90  100

如果你想将其重塑为 50x2 数组,我们可以尝试

> apply(m, 2, `[`)
      [,1] [,2]
 [1,]    1   11
 [2,]    2   12
 [3,]    3   13
 [4,]    4   14
 [5,]    5   15
 [6,]    6   16
 [7,]    7   17
 [8,]    8   18
 [9,]    9   19
[10,]   10   20
[11,]   21   31
[12,]   22   32
[13,]   23   33
[14,]   24   34
[15,]   25   35
[16,]   26   36
[17,]   27   37
[18,]   28   38
[19,]   29   39
[20,]   30   40
[21,]   41   51
[22,]   42   52
[23,]   43   53
[24,]   44   54
[25,]   45   55
[26,]   46   56
[27,]   47   57
[28,]   48   58
[29,]   49   59
[30,]   50   60
[31,]   61   71
[32,]   62   72
[33,]   63   73
[34,]   64   74
[35,]   65   75
[36,]   66   76
[37,]   67   77
[38,]   68   78
[39,]   69   79
[40,]   70   80
[41,]   81   91
[42,]   82   92
[43,]   83   93
[44,]   84   94
[45,]   85   95
[46,]   86   96
[47,]   87   97
[48,]   88   98
[49,]   89   99
[50,]   90  100

Let's start from a smaller example with array m

m <- array(rep(1:100, 5), c(10, 2, 5))

which is a 3-D array

> m
, , 1

      [,1] [,2]
 [1,]    1   11
 [2,]    2   12
 [3,]    3   13
 [4,]    4   14
 [5,]    5   15
 [6,]    6   16
 [7,]    7   17
 [8,]    8   18
 [9,]    9   19
[10,]   10   20

, , 2

      [,1] [,2]
 [1,]   21   31
 [2,]   22   32
 [3,]   23   33
 [4,]   24   34
 [5,]   25   35
 [6,]   26   36
 [7,]   27   37
 [8,]   28   38
 [9,]   29   39
[10,]   30   40

, , 3

      [,1] [,2]
 [1,]   41   51
 [2,]   42   52
 [3,]   43   53
 [4,]   44   54
 [5,]   45   55
 [6,]   46   56
 [7,]   47   57
 [8,]   48   58
 [9,]   49   59
[10,]   50   60

, , 4

      [,1] [,2]
 [1,]   61   71
 [2,]   62   72
 [3,]   63   73
 [4,]   64   74
 [5,]   65   75
 [6,]   66   76
 [7,]   67   77
 [8,]   68   78
 [9,]   69   79
[10,]   70   80

, , 5

      [,1] [,2]
 [1,]   81   91
 [2,]   82   92
 [3,]   83   93
 [4,]   84   94
 [5,]   85   95
 [6,]   86   96
 [7,]   87   97
 [8,]   88   98
 [9,]   89   99
[10,]   90  100

If you want to reshape it into a 50x2 array, we can try

> apply(m, 2, `[`)
      [,1] [,2]
 [1,]    1   11
 [2,]    2   12
 [3,]    3   13
 [4,]    4   14
 [5,]    5   15
 [6,]    6   16
 [7,]    7   17
 [8,]    8   18
 [9,]    9   19
[10,]   10   20
[11,]   21   31
[12,]   22   32
[13,]   23   33
[14,]   24   34
[15,]   25   35
[16,]   26   36
[17,]   27   37
[18,]   28   38
[19,]   29   39
[20,]   30   40
[21,]   41   51
[22,]   42   52
[23,]   43   53
[24,]   44   54
[25,]   45   55
[26,]   46   56
[27,]   47   57
[28,]   48   58
[29,]   49   59
[30,]   50   60
[31,]   61   71
[32,]   62   72
[33,]   63   73
[34,]   64   74
[35,]   65   75
[36,]   66   76
[37,]   67   77
[38,]   68   78
[39,]   69   79
[40,]   70   80
[41,]   81   91
[42,]   82   92
[43,]   83   93
[44,]   84   94
[45,]   85   95
[46,]   86   96
[47,]   87   97
[48,]   88   98
[49,]   89   99
[50,]   90  100

您只需设置 dim 属性即可。

让我们以 3 x 2 x 3 数组为例:

example <- array(1:18, c(3, 2, 3))

example
#> , , 1
#> 
#>      [,1] [,2]
#> [1,]    1    4
#> [2,]    2    5
#> [3,]    3    6
#> 
#> , , 2
#> 
#>      [,1] [,2]
#> [1,]    7   10
#> [2,]    8   11
#> [3,]    9   12
#> 
#> , , 3
#> 
#>      [,1] [,2]
#> [1,]   13   16
#> [2,]   14   17
#> [3,]   15   18

要分成 2 列,我们可以这样做:

dim(example) <- c(9, 2)

example
#>       [,1] [,2]
#>  [1,]    1   10
#>  [2,]    2   11
#>  [3,]    3   12
#>  [4,]    4   13
#>  [5,]    5   14
#>  [6,]    6   15
#>  [7,]    7   16
#>  [8,]    8   17
#>  [9,]    9   18

reprex 包 (v2.0.1)

You can simply set the dim attribute.

Let's take an example of a 3 x 2 x 3 array:

example <- array(1:18, c(3, 2, 3))

example
#> , , 1
#> 
#>      [,1] [,2]
#> [1,]    1    4
#> [2,]    2    5
#> [3,]    3    6
#> 
#> , , 2
#> 
#>      [,1] [,2]
#> [1,]    7   10
#> [2,]    8   11
#> [3,]    9   12
#> 
#> , , 3
#> 
#>      [,1] [,2]
#> [1,]   13   16
#> [2,]   14   17
#> [3,]   15   18

To get in into 2 columns, we can do:

dim(example) <- c(9, 2)

example
#>       [,1] [,2]
#>  [1,]    1   10
#>  [2,]    2   11
#>  [3,]    3   12
#>  [4,]    4   13
#>  [5,]    5   14
#>  [6,]    6   15
#>  [7,]    7   16
#>  [8,]    8   17
#>  [9,]    9   18

Created on 2022-04-02 by the reprex package (v2.0.1)

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