2

I have nested list as follows,

 dput( list(structure(c("123.60", " on"))))

I am interested in converting the elements in this nested list into a dataframe. For example, the output should be as follows.

      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 

Need help converting these elements into a dataframe.

3
  • 1
    Try using dput in order to make your problem reproducible Commented Jan 13, 2015 at 20:32
  • 1
    @DavidArenburg, thanks David, I did that now. Commented Jan 13, 2015 at 20:47
  • @Science11, did you mean to delete the output of dput when you edited your question? Commented Jan 14, 2015 at 6:16

1 Answer 1

6

That's not exactly a nested list, but a list of named character vectors. You can apply as.data.frame.list to each element, then use rbind. So if x is your list, then

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

Update : You can also do

data.frame(do.call(rbind, x), stringsAsFactors=FALSE)

And other, likely more efficient, possibilities include

library(data.table)
rbindlist(lapply(x, as.list))

and

library(dplyr)
bind_rows(lapply(x, as.data.frame.list, stringsAsFactors=FALSE))

and (thanks to Ananda Mahto)

library(stringi)
data.frame(stri_list2matrix(x, byrow=TRUE), stringsAsFactors=FALSE)

All of these would still require a type conversion on the first column if you wish for it to be numeric.

Also, the data from this question seems to have disappeared, so here it is, copied from the edit history.

 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")))   
Sign up to request clarification or add additional context in comments.

9 Comments

@Science11 - good! I added type.convert to make sure numeric columns end up numerics, and not characters
Never heard of type.convert before...
@DavidArenburg - It's pretty neat. It attempts conversion on character vectors to their appropriate types. Actually now that I think about it, it might not be appropriate here because it just converted the strings back to factors again. Haha
Yeah thanks, I know how to read documentation :) Where did you find this gem?
@DavidArenburg - Actually I first saw it in Ananda Mahto's code
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.