R 将维度可变的嵌套列表转换为数据帧
我有一个嵌套列表,我正试图将其转换为数据帧。我花了好几个小时检查这里的解决方案,但什么都不管用 下面是我用来生成此示例的代码:R 将维度可变的嵌套列表转换为数据帧,r,nested-lists,R,Nested Lists,我有一个嵌套列表,我正试图将其转换为数据帧。我花了好几个小时检查这里的解决方案,但什么都不管用 下面是我用来生成此示例的代码: #library('devtools') #install_github('blockspring/blockspring.R') library(blockspring) library(rjson) comp <- blockspringRunParsed("alternative-to-search", list( "query" = "Qualtr
#library('devtools')
#install_github('blockspring/blockspring.R')
library(blockspring)
library(rjson)
comp <- blockspringRunParsed("alternative-to-search", list(
"query" = "Qualtrics",
"page_number" = NULL
), list(
"api_key" = "br_2884_cc148ba53e81b7b3eeaf1ac65bc539cf62fe9850"))$params
我尝试过的一件事是带有data.table包的rbindlist函数
> rbindlist(comp, use.names=T, fill=T)
但我收到了这个错误:
Error in rbindlist(comp, use.names = T, fill = T) :
fill=TRUE, but names of input list at position 1 is NULL. All items of input list must have names set when fill=TRUE.
我还从qdap软件包中尝试了这一点:
> head(mtabulate(lapply(comp, `[[`,"results")))
它返回:
data frame with 0 columns and 1 row
Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
当我尝试将其转换为数据帧时,它会创建一行df。理想情况下,数据帧将有25行6列。有一些缺少“$icon”项,因此对于缺少值的项目,应输入“NA”
任何帮助都会很棒!
谢谢
您可以使用
plyr
库中的rbind.fill
library(plyr)
do.call(rbind.fill, lapply(unlist(comp, rec=F), as.data.frame))
如果您共享的数据不是可复制的格式,那么将有助于包含一个信息。此外,尽可能精确地描述示例输入的所需输出。第一行创建了一个df,其中有一行和148个变量(这是不正确的)。第二行给出了一个错误“rbind中的错误(deparse.level,…):参数列数不匹配”您没有提供可复制的数据来尝试不同的解决方案,那么您希望有人如何为您提供所需的输出@NathanCheever我已经添加了生成输出的代码,所以我希望这会有所帮助。@NathanCheever您有没有尝试过第三个选项?这似乎适用于您添加的示例。我不完全确定您所说的“第三种选择”是什么意思。这是否意味着提交了一份答复?(这给了我一个错误:error:rbind.fill的所有输入必须是data.frames)
> sessionInfo()
R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] qdap_2.2.0 RColorBrewer_1.1-2 qdapTools_1.1.0
[4] qdapRegex_0.2.0 qdapDictionaries_1.0.2 ggplot2_1.0.0
[7] data.table_1.9.4 plyr_1.8.1 rjson_0.2.15
[10] blockspring_0.4
loaded via a namespace (and not attached):
[1] assertthat_0.1 bitops_1.0-6 chron_2.3-45
[4] colorspace_1.2-5 DBI_0.3.1 devtools_1.7.0
[7] digest_0.6.8 dplyr_0.4.1 gdata_2.13.3
[10] gender_0.4.3 grid_3.1.3 gridExtra_0.9.1
[13] gtable_0.1.2 gtools_3.4.1 httr_0.6.1
[16] igraph_0.7.1 jsonlite_0.9.14 magrittr_1.5
[19] MASS_7.3-39 mime_0.2 munsell_0.4.2
[22] NLP_0.1-6 openNLP_0.2-4 openNLPdata_1.5.3-1
[25] parallel_3.1.3 plotrix_3.5-11 proto_0.3-10
[28] R.methodsS3_1.7.0 R.oo_1.19.0 R.utils_2.0.2
[31] Rcpp_0.11.5 RCurl_1.95-4.5 reports_0.1.4
[34] reshape2_1.4.1 rJava_0.9-6 scales_0.2.4
[37] slam_0.1-32 stringr_0.6.2 tcltk_3.1.3
[40] tm_0.6 tools_3.1.3 venneuler_1.1-0
[43] wordcloud_2.5 xlsx_0.5.7 xlsxjars_0.6.1
[46] XML_3.98-1.1
library(plyr)
do.call(rbind.fill, lapply(unlist(comp, rec=F), as.data.frame))