R中与cbind()函数相反的函数是什么?

3
假设我有一个矩阵列表:
matrix <- matrix(1:4, nrow = 2, ncol = 2)
list <- list(matrix, matrix, matrix)

使用函数cbind()创建的矩阵:

long.matrix <- do.call(cbind, list)

      [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    3    1    3    1    3
[2,]    2    4    2    4    2    4

我想要反向处理以从“long.matrix”获取矩阵列表。
我可以使用“for”循环手动完成,但我正在搜索类似于:function(long.matrix, 3) 的东西,我认为应该存在。有这样的东西吗?

非常接近 https://dev59.com/PVoU5IYBdhLWcg3wxIyA。 - Pierre L
3个回答

5

暴力解决方案:

f <- function(long.matrix, num)
           lapply(split(long.matrix, 
                        rep(seq(num), each=(ncol(long.matrix)/num)*nrow(long.matrix))), 
                  function(x) matrix(x, nrow=nrow(long.matrix))
           )

f(long.matrix, 3)
## $`1`
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## 
## $`2`
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## 
## $`3`
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4

rep函数为split构建类别,以便分割数据。由于R是按列排列的,因此我们在此将前四个、第二个四个和第三个四个条目取出。

填充当前示例long.matrix3维度的值,该函数简化为:

lapply(split(long.matrix, rep(seq(3), each=4)), function(x) matrix(x, nrow=2))

注意:
(r <- rep(seq(3), each=4) )
## [1] 1 1 1 1 2 2 2 2 3 3 3 3
split(long.matrix, r)
## $`1`
## [1] 1 2 3 4
## 
## $`2`
## [1] 1 2 3 4
## 
## $`3`
## [1] 1 2 3 4

每个值都会被传递给matrix函数以获得所需的格式。


谢谢!我大概明白了 :) 你认为在基本的R语言中没有实现这样的功能吗? - cure
@cure 我不认为有,但我很乐意被证明是错误的。 - Matthew Lundberg
谢谢你提供的解决方案!答案看起来很完整。这就是我找不到直接解决方案的原因。我应该编辑问题,说明它可能不存在,需要手动完成吗? - cure
1
@cure 我认为这并不是必要的。当寻求解决方案时,内置或包中的解决方案(几乎)总是首选,但编写代码来完成工作通常也是可以接受的。 - Matthew Lundberg

2
做这个:
listm=list()  #i=1
for(i in 1:3)listm[[i]]=long.matrix[,(2*i-1):(i*2)]
lapply(1:3,function(ii)long.matrix[,(2*ii-1):(ii*2)])

[[1]]
     [,1] [,2]
[1,]    1    3
[2,]    2    4

[[2]]
     [,1] [,2]
[1,]    1    3
[2,]    2    4

[[3]]
     [,1] [,2]
[1,]    1    3
[2,]    2    4

2

我建议使用数组维度来实现。然后,您可以为矩阵定义一个 split 方法:

split.matrix <- function(x, rslice = 1, cslice = 1) {
  if (ncol(x) %% cslice) stop("cslice not divisor of number of columns")
  if (nrow(x) %% rslice) stop("rslice not divisor of number of rows")

  x <- t(x)
  dim(x) <- c(dim(x)[1],
              dim(x)[2] / rslice,
              rslice)
  x <- lapply(seq_len(rslice), function(k, a) t(a[,,k]), a = x)

  if (cslice > 1) {
    x <- lapply(x, function(y, k) {

      dim(y) <- c(dim(y)[1],
                  dim(y)[2] / k,
                  k)
      y <- lapply(seq_len(k), function(k, a) a[,,k], a = y)
      y
    }, k = cslice)
  }
  if(length(x) == 1L) x <- x[[1]]

  x
}

split(long.matrix, 1, 3)
#[[1]]
#     [,1] [,2]
#[1,]    1    3
#[2,]    2    4
#
#[[2]]
#     [,1] [,2]
#[1,]    1    3
#[2,]    2    4
#
#[[3]]
#     [,1] [,2]
#[1,]    1    3
#[2,]    2    4

split(long.matrix, 1, 1)
#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    1    3    1    3    1    3
#[2,]    2    4    2    4    2    4

split(long.matrix, 2, 1)

#[[1]]
#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    1    3    1    3    1    3
#
#[[2]]
#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    2    4    2    4    2    4

split(long.matrix, 2, 3)
#[[1]]
#[[1]][[1]]
#[1] 1 3
#
#[[1]][[2]]
#[1] 1 3
#
#[[1]][[3]]
#[1] 1 3
#
#
#[[2]]
#[[2]][[1]]
#[1] 2 4
#
#[[2]][[2]]
#[1] 2 4
#
#[[2]][[3]]
#[1] 2 4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接