如何在R中创建列表矩阵?

aca*_*ser 7 arrays r list matrix

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

1       2       3
1  1,2,4  1,2      1
2  Null   3,4,5,6  1,3  
Run Code Online (Sandbox Code Playgroud)

我看到这篇文章,并尝试了以下但有一个错误:

 b <- array()
 b[j, i, ] <- A[i]
Run Code Online (Sandbox Code Playgroud)

其中A是矢量本身.错误是:

 Error in b[j, i, ] <- A[i] : incorrect number of subscripts
Run Code Online (Sandbox Code Playgroud)

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

Update1:

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

Error in b[j, i] <- A[i] : replacement has length zero
Run Code Online (Sandbox Code Playgroud)

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

Update2:

 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
Run Code Online (Sandbox Code Playgroud)

:

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)
Run Code Online (Sandbox Code Playgroud)

A:第1行的值,F:第2行和第3行:第3行(第5列)

这个数据不是这种形式,也不是存储在任何地方,它们是另一个功能的输出(有功能代替A[i]).数据只显示A重复的剂量,因此显示矩阵中的位置并给出背errorUPDATE2.A[4]是第4列第2行的元素.

42-*_*42- 14

这会构建该矩阵,尽管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
Run Code Online (Sandbox Code Playgroud)

检查第一个元素:

> 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"
Run Code Online (Sandbox Code Playgroud)

从列表中创建"列表矩阵"的演示:

> 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"
Run Code Online (Sandbox Code Playgroud)

进一步要求示范:

 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        
Run Code Online (Sandbox Code Playgroud)

你问(在评论中)"....有没有办法定义列和行的数量,但不是元素本身,因为它们是未知的?"

回答(最初在评论中)

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) 
Run Code Online (Sandbox Code Playgroud)

  • 就像我说的...需要使用“ [[”:`b [[j * nrow(b)+ i]]` (2认同)