将嵌套列表中的元素转换为数据帧

Sci*_*e11 2 r list dataframe

我有如下嵌套列表,

 dput( list(structure(c("123.60", " on"))))
Run Code Online (Sandbox Code Playgroud)

我有兴趣将此嵌套列表中的元素转换为数据帧.例如,输出应如下所示.

      code      description      
      123.60    not stated as uncontrolled, with neurological manifestations
      123.50    not stated as uncontrolled, with ophthalmic manifestations
      .
      .
      .
      123.52    uncontrolled, with ophthalmic manifestations 
Run Code Online (Sandbox Code Playgroud)

需要帮助将这些元素转换为数据框.

Ric*_*ven 6

这不是一个嵌套列表,而是一个命名字符向量列表.您可以应用于as.data.frame.list每个元素,然后使用rbind.那么,如果x是你的清单,那么

df <- do.call(rbind, lapply(x, as.data.frame.list, stringsAsFactors = FALSE))
## below is optional - converts character columns to appropriate type
## but will also convert some columns back to factors again
df[] <- lapply(df, type.convert) 
df
#      code                                                   description codeSystem codeSystemVersion
# 1  123.60  not stated as uncontrolled, with neurological manifestations     XAZ9CM       XAZ9CM-2012
# 2  123.50    not stated as uncontrolled, with ophthalmic manifestations     XAZ9CM       XAZ9CM-2012
# 3  123.61  not stated as uncontrolled, with neurological manifestations     XAZ9CM       XAZ9CM-2012
# 4   123.7                              peripheral circulatory disorders     XAZ9CM       XAZ9CM-2012
# 5  123.40         not stated as uncontrolled, with renal manifestations     XAZ9CM       XAZ9CM-2012
# 6  123.41         not stated as uncontrolled, with renal manifestations     XAZ9CM       XAZ9CM-2012
# 7   123.5                                     ophthalmic manifestations     XAZ9CM       XAZ9CM-2012
# 8  123.53                  uncontrolled, with ophthalmic manifestations     XAZ9CM       XAZ9CM-2012
# 9  123.52                  uncontrolled, with ophthalmic manifestations     XAZ9CM       XAZ9CM-2012
# 10  123.4                                          renal manifestations     XAZ9CM       XAZ9CM-2012
Run Code Online (Sandbox Code Playgroud)

更新:你也可以

data.frame(do.call(rbind, x), stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)

其他可能更有效的可能性包括

library(data.table)
rbindlist(lapply(x, as.list))
Run Code Online (Sandbox Code Playgroud)

library(dplyr)
bind_rows(lapply(x, as.data.frame.list, stringsAsFactors=FALSE))
Run Code Online (Sandbox Code Playgroud)

(感谢Ananda Mahto)

library(stringi)
data.frame(stri_list2matrix(x, byrow=TRUE), stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)

如果您希望它是数字,所有这些仍然需要在第一列上进行类型转换.

此外,来自这个问题的数据似乎已经消失,所以在这里,它是从编辑历史中复制的.

 x <- list(structure(c("123.60", " not stated as uncontrolled, with neurological manifestations",                 
     "XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",                                    
     "codeSystemVersion")), structure(c("123.50", " not stated as uncontrolled, with ophthalmic manifestations",  
     "XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",                                    
     "codeSystemVersion")), structure(c("123.61", "not stated as uncontrolled, with neurological manifestations", 
     "XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",                                    
     "codeSystemVersion")), structure(c("123.7", "peripheral circulatory disorders",                              
     "XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",                                    
     "codeSystemVersion")), structure(c("123.40", " not stated as uncontrolled, with renal manifestations",       
     "XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",                                    
     "codeSystemVersion")), structure(c("123.41", " not stated as uncontrolled, with renal manifestations",       
     "XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",                                    
     "codeSystemVersion")), structure(c("123.5", "ophthalmic manifestations",                                     
     "XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",                                    
     "codeSystemVersion")), structure(c("123.53", "uncontrolled, with ophthalmic manifestations",                 
     "XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",                                    
     "codeSystemVersion")), structure(c("123.52", " uncontrolled, with ophthalmic manifestations",                
     "XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",                                    
     "codeSystemVersion")), structure(c("123.4", "renal manifestations",                                          
     "XAZ9CM", "XAZ9CM-2012"), .Names = c("code", "description", "codeSystem",                                    
     "codeSystemVersion")))   
Run Code Online (Sandbox Code Playgroud)