用R将矩阵划分为N个相等大小的块 [英] Partition matrix into N equally-sized chunks with R

查看:126
本文介绍了用R将矩阵划分为N个相等大小的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用R将矩阵或数据帧划分为N个大小相等的块?我想水平切割矩阵或数据框.

How can I partition a matrix or dataframe into N equally-sized chunks with R? I want to cut the matrix or dataframe horizontally.

例如,给定:

r = 8
c = 10
number_of_chunks = 4
data = matrix(seq(r*c), nrow = r, ncol=c)
>>> data

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    9   17   25   33   41   49   57   65    73
[2,]    2   10   18   26   34   42   50   58   66    74
[3,]    3   11   19   27   35   43   51   59   67    75
[4,]    4   12   20   28   36   44   52   60   68    76
[5,]    5   13   21   29   37   45   53   61   69    77
[6,]    6   14   22   30   38   46   54   62   70    78
[7,]    7   15   23   31   39   47   55   63   71    79
[8,]    8   16   24   32   40   48   56   64   72    80

我想将data切成4个元素的列表:

I would like to have to cut data into a list of 4 elements:

元素1:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    9   17   25   33   41   49   57   65    73
[2,]    2   10   18   26   34   42   50   58   66    74

元素2:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[3,]    3   11   19   27   35   43   51   59   67    75
[4,]    4   12   20   28   36   44   52   60   68    76

元素3:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[5,]    5   13   21   29   37   45   53   61   69    77
[6,]    6   14   22   30   38   46   54   62   70    78

元素4:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[7,]    7   15   23   31   39   47   55   63   71    79
[8,]    8   16   24   32   40   48   56   64   72    80

在python中使用numpy时,我可以使用 numpy.array_split .

With numpy in python, I can use numpy.array_split.

推荐答案

这是在基数R中的尝试.使用pretty计算行序列的漂亮"切割值.用cut对行号的序列进行分类,并使用split返回在切割值处分割的序列的列表.最后,使用lapply浏览拆分行值的列表,并使用[提取矩阵子集.

Here's an attempt in base R. Calculate "pretty" cut values for the sequence of rows using pretty. Categorized the sequence of row numbers with cut and return a list of the the sequence split at the cut values with split. Finally, run through a list of the split row values using lapply extract the matrix subsets with [.

lapply(split(seq_len(nrow(data)),
             cut(seq_len(nrow(data)), pretty(seq_len(nrow(data)), number_of_chunks))),
       function(x) data[x, ])
$`(0,2]`
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    9   17   25   33   41   49   57   65    73
[2,]    2   10   18   26   34   42   50   58   66    74

$`(2,4]`
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    3   11   19   27   35   43   51   59   67    75
[2,]    4   12   20   28   36   44   52   60   68    76

$`(4,6]`
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    5   13   21   29   37   45   53   61   69    77
[2,]    6   14   22   30   38   46   54   62   70    78

$`(6,8]`
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    7   15   23   31   39   47   55   63   71    79
[2,]    8   16   24   32   40   48   56   64   72    80

将其滚动为一个函数:

array_split <- function(data, number_of_chunks) {
  rowIdx <- seq_len(nrow(data))    
  lapply(split(rowIdx, cut(rowIdx, pretty(rowIdx, number_of_chunks))), function(x) data[x, ])
}

然后,您可以使用

array_split(data=data, number_of_chunks=number_of_chunks)

返回与上面相同的结果.

to return the same result as above.

@ user20650建议的一个很好的简化是

A nice simplification suggested by @user20650 is

split.data.frame(data,
                 cut(seq_len(nrow(data)), pretty(seq_len(nrow(data)), number_of_chunks)))

令我惊讶的是,split.data.frame当第一个参数是矩阵时会返回一个矩阵列表.

A surprise to me, split.data.frame returns a list of matrices when its first argument is a matrix.

这篇关于用R将矩阵划分为N个相等大小的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆