如何在R中创建列表矩阵? [英] How to create a matrix of lists in R?

查看:180
本文介绍了如何在R中创建列表矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What i want to have is a matrix in which each element is a list itself. See the following example:

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

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

I saw this post, and tried the following but got an error :

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

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

where A is a vector itself. The error was:

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

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

How should I define and access each element of the matrix and each element of the contained lists?

更新1:

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

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

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

I want to specify that each element is a list and then try to fill it with various list with different length from zero to 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

A :

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 :values of row 1 , F:row 2 and E :row 3. ( 5 column )

此数据不是这种形式,也不存储在任何地方,它们是另一个函数的输出(在A[i]处有函数).该数据仅显示了A的剂量可重复性,并且因此会显示矩阵中的位置并在 update2 中返回error.A[4]是第4列第2行的元素.

this data is not in this form and is not stored any where,they are the output of another function (there is function in the place of A[i]).the data just show what dose A look likes reproducibly and therefore shows the position in the matrix and gives back the error in update2.A[4] is the element of column 4 row 2.

推荐答案

尽管打印方法不能按照您想象的方式显示矩阵,但这可以构建矩阵:

This builds that matrix although the print method does not display it in the manner you imagined:

 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"

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

Demonstration of creating "matrix of lists" from a 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        

您问(在评论中)"....是否可以定义列数和行数,但不能定义元素本身,因为它们是未知的?"

You asked (in comments) "....is there a way to define number of column and rows , but not the element itself because they are unknown?"

已回答(最初在评论中)

Answered (initially in comments)

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) 

这篇关于如何在R中创建列表矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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