【问题标题】:Statement that produces list with "int" structure?生成具有“int”结构的列表的语句?
【发布时间】:2018-03-04 15:29:23
【问题描述】:

我正在熟悉 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】:

    类似的东西:

    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 结构,则元素是vectorsinteger 类型。因此,您应该在 list 函数中传递整数向量。

    【讨论】:

    • 感谢您的回复,我尝试将 as.integer() 应用于我的数值,结果确实是 $ : int [,] 结构,但我没有注意到 vector 类型的元素(谢谢提出这个问题!)。
    猜你喜欢
    • 2015-01-05
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多