【问题标题】:How to create a matrix of lists in R?如何在 R 中创建列表矩阵?
【发布时间】:2015-07-12 12:37:27
【问题描述】:

我想要的是一个矩阵,其中每个元素都是一个列表 本身。 请参阅以下示例:

1       2       3
1  1,2,4  1,2      1
2  Null   3,4,5,6  1,3  

我看到了this 的帖子,并尝试了以下操作但出现错误:

 b <- array()
 b[j, i, ] <- A[i]

其中 A 是一个向量本身。 错误是:

 Error in b[j, i, ] <- A[i] : incorrect number of subscripts

我应该如何定义和访问矩阵的每个元素以及包含列表的每个元素?

更新1:

b<-matrix(list(),nrow = length(d), ncol =length(c))

Error in b[j, i] <- A[i] : replacement has length zero

我想指定每个元素都是一个列表,然后尝试用从零到 n 不同长度的各种列表填充它。

更新2:

 running what @BondedDust commented :
 b<-matrix(rep(list(),(c*d)),,nrow = length(d), ncol =length(c))
 Erorr in b[[j*nrow(b)+i]] <- A[i] : attempt to select less than one element

一个

A[1]<-c(3)     F[[1]]<-numeric(0)   E[[1]]<-numeric(0)
A[2]<-c(1)     F[2]<-c(1)           E[2]<-c(1)
A[3]<-c(1)     F[3]<-c(2)           E[[3]]<-numeric(0)
A[[4]]<-c(1,3) F[[4]]<-numeric(0)   E[[4]]<-numeric(0)
A[5]<-c(4)     F[5]<-c(4)           E[5]<-c(4)

A:第 1 行、F:第 2 行和 E:第 3 行的值。(第 5 列)

这个数据不是这种形式,也没有存储在任何地方,它们是另一个函数的输出(有函数代替A[i])。数据只是显示了A看起来可重复的剂量和因此显示矩阵中的位置并返回 update2 中的errorA[4] 是第 4 列第 2 行的元素。

【问题讨论】:

  • @DavidArenburg 不是 matrix(list(c(1,2,3))) 他需要的吗?
  • 矩阵中并非不可能有列表元素。
  • matrix 的文档并没有说只能将原子向量作为参数提供给 data 参数。它只说as.vector 将删除属性。 (去除了它们类的 Dates、POSIXct 和 factor 参数。)如果它们只是列表(毕竟是另一种形式的“R 向量”),那么对数据没有暴力。
  • 编辑。将对象设置为空数组不允许您进行动态数组尺寸标注。这可能是其他语言中数组的一个特性,但在 R 中不是。
  • 此外,正如我之前解释的,R 矩阵在 column-major order 中被索引。我想你可以随时使用t() 函数,如果需要像你描述的那样赦免的话。

标签: arrays r list matrix


【解决方案1】:

这会构建该矩阵,尽管 print 方法不会以您想象的方式显示它:

 matrix( list(c(1,2,4), c(NULL), c(1,2), c(3,4,5,6), c(1), c(1,3)), 2,3)
 #---------
     [,1]      [,2]      [,3]     
[1,] Numeric,3 Numeric,2 1        
[2,] NULL      Numeric,4 Numeric,2

检查第一个元素:

> Mlist <- matrix( list(c(1,2,4), c(NULL), c(1,2), c(3,4,5,6), c(1), c(1,3)), 2,3)
> Mlist[1,1]
[[1]]
[1] 1 2 4

> is.matrix(Mlist)
[1] TRUE
> class( Mlist[1,1] )
[1] "list"

从列表创建“列表矩阵”的演示:

> will.become.a.matrix <- list(c(1,2,4), c(NULL), c(1,2), c(3,4,5,6), c(1), c(1,3))
> is.matrix(will.become.a.matrix)
[1] FALSE
> dim(will.become.a.matrix) <- c(2,3)
> is.matrix(will.become.a.matrix)
[1] TRUE
> dim(will.become.a.matrix)
[1] 2 3
> class(will.become.a.matrix[1,1])
[1] "list"

进一步要求的演示:

 A<- list(); F=list() E=list()
 A[1]<-c(3) ;  F[[1]]<-numeric(0);  E[[1]]<-numeric(0)
 A[2]<-c(1) ;  F[2]<-c(1)   ;        E[2]<-c(1)
 A[3]<-c(1) ;  F[3]<-c(2)  ;         E[[3]]<-numeric(0)
 A[[4]]<-list(1,3) ;F[[4]]<-numeric(0) ; E[[4]]<-numeric(0)
 A[5]<-c(4) ; F[5]<-c(4)       ;    E[5]<-c(4)
 Mlist= c(A,F,E)
 M <- matrix(Mlist, length(A), 3)
#=====================================
> M
     [,1]   [,2]      [,3]     
[1,] 3      Numeric,0 Numeric,0
[2,] 1      1         1        
[3,] 1      2         Numeric,0
[4,] List,2 Numeric,0 Numeric,0
[5,] 4      4         4        

您问(在 cmets 中)“....有没有办法定义列数和行数,而不是元素本身,因为它们是未知的?”

已回答(最初在 cmets 中)

b<-matrix(rep(list(), 6),nrow = 2, ncol =3) 
#.... then replace the NULL items with values. 
# Need to use "[[": for assignment (which your 'Update 1' did not 
# ....and your Update2 only did for some but not all of the assignments.)

b[[1]] <- c(1,2,3,4) 

【讨论】:

  • @jhin:在我向您展示第一个元素是一个列表之后,我不明白您如何保持那个不正确的位置。
  • 根据需要创建一个包含尽可能多的元素的列表。确保元素按“列主要顺序”排列,然后使用 dim&lt;-() 分配 m x n 的维度
  • 当函数参数返回的值是一个列表时,从tapply获取一个矩阵或列表数组是很常见的。
  • b&lt;-matrix(rep(list(), 6),nrow = 2, ncol =3) .... 然后用值替换 NULL 项。需要使用“[[”:b[[1]] &lt;- c(1,2,3,4)
  • 正如我所说...需要使用“[[”:b[[ j*nrow(b)+i]]
猜你喜欢
  • 2012-11-25
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
相关资源
最近更新 更多