2015-05-27 32 views
0

我有一個嵌套列表,我試圖將其轉換爲數據框架。我花了幾個小時在這裏檢查解決方案,沒有爲我工作。將具有不同維度的嵌套列表轉換爲數據框

這裏是我用來生成這個例子的代碼:

#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 

這將創建一個在我的環境被稱爲「補償」與嵌套列表清單

> str(comp) 
    List of 1 
    $ results:List of 25 
     ..$ :List of 6 
    .. ..$ icon  : chr "http://cdn.altrn.tv/icons/google-forms_53250.png?width=32&height=32&mode=crop&anchor=middlecenter" 
     .. ..$ likes  : chr "29" 
     .. ..$ name  : chr "Google Drive - Forms" 
     .. ..$ pageLink : chr "http://alternativeto.net/software/google-forms/" 
     .. ..$ projectType: chr "Free" 
     .. ..$ description: chr "Google Forms, part of Google Drive , is a tool... 
    ...several more results returned..... 
    ..$ :List of 6 
      .. ..$ icon  : chr "http://cdn.altrn.tv/icons/webform_19004.png?width=32&height=32&mode=crop&anchor=middlecenter" 
      .. ..$ likes  : chr "2" 
      .. ..$ name  : chr "Webform" 
      .. ..$ pageLink : chr "http://alternativeto.net/software/webform/" 
      .. ..$ projectType: chr "Freemium" 
      .. ..$ description: chr "Free surveys that can go with you anywhere. Build ------ Make surveys crazy-fast with the Webform builder. Multiple pages,"| __truncated__ 
      ..$ :List of 5 
      .. ..$ likes  : chr "1" 
      .. ..$ name  : chr "Factile" 
      .. ..$ pageLink : chr "http://alternativeto.net/software/factile/" 
      .. ..$ projectType: chr "Free" 
      .. ..$ description: chr "Factile is free online survey building 

有一兩件事我想是的rbindlist函數與data.table包

> 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」。

任何幫助將是太棒了! 感謝

> 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 
+1

這將有助於包括[重複的例子(http://stackoverflow.com/questions/5963269/how-to-make -a-great-r-reproducible-example)您共享的數據不是可重現的格式。此外,儘可能精確地關於樣本輸入的期望輸出。 – MrFlick

回答

0

您可以使用rbind.fillplyr

library(plyr) 
do.call(rbind.fill, lapply(unlist(comp, rec=F), as.data.frame)) 
+0

第一行用一行和148個變量創建一個df(這是不正確的)。第二行給出了一個錯誤「在rbind錯誤(deparse.level,...): 號的參數不匹配列」 – cheevahagadog

+2

您沒有提供可重複的數據來嘗試不同的解決方案,所以你怎麼能指望別人給你所需的輸出? @NathanCheever –

+1

我已經添加了我的代碼來生成輸出,所以我希望這有助於。 – cheevahagadog

相關問題