产生具有“int”结构的列表的语句?

问题描述 投票:1回答:1

我熟悉Jonathan Chang和共同作者的lda R软件包,准备对我自己进行类似的分析。我检查了内置的cora.documentscora.cites数据的结构,它们以列表格式存储,但是像这样

library(lda)
str(cora.cites)
List of 2410
 $ : int [1:2] 484 389
 $ : int(0) 
 $ : int(0) 
 $ : int [1:3] 177 416 533
 $ : int 153
 $ : int(0) 
 $ : int(0) 
 $ : int(0) 
 $ : int(0) 
 $ : int(0) 
 $ : int(0) 
 $ : int 139
 $ : int 433
 $ : int [1:16] 233 391 350 208 484 666 218 630 28 656 ...
 $ : int [1:8] 210 624 229 136 356 228 289 73
 $ : int(0) 

哪里

class(cora.cites)
[1] "list"
class(cora.cites[1])
[1] "list"

但是,如果我要构建类似结构的数据,例如,

list1 <- list(484, 389)
list2 <- list(0)
list3 <- list(0)
list4 <- list(177, 416, 533)
my_list <- list(list1, list2, list3, list4)

结果是

str(my_list)
List of 4
 $ :List of 2
  ..$ : num 484
  ..$ : num 389
 $ :List of 1
  ..$ : num 0
 $ :List of 1
  ..$ : num 0
 $ :List of 3
  ..$ : num 177
  ..$ : num 416
  ..$ : num 533

没有附带int和尺寸[,]信息列为cora.cites数据。

如何生成列表格式的数据,其结构类似于lda包的样本数据?

r list int lda
1个回答
1
投票

有点像:

x<-list(integer(5),integer(5))
str(x)
# List of 2
# $ : int [1:5] 0 0 0 0 0
# $ : int [1:5] 0 0 0 0 0

如果你观察你做了什么,你创建了lists并将它们包含在list中。因此,您获得了列表作为my_list的元素。但是,如果你检查cora.sites结构,元素是vectors类型的integer。因此,您应该在list函数中传递整数向量。

© www.soinside.com 2019 - 2024. All rights reserved.