recursion - Convert recursive lists to dynamic data.frame in R -


i need build function traverses lists , sublists recursively , convert data.frame. let's have list that

from <- list(id="12345678", name="gabriele") message <- "the quick fox" comments <- list(list(name="mickey", comment="hello world!"), list(name="donald", message="world...hello!"))  big.list <- list(from, message, comments) 

i need convert in form of data.frame schema

from_id, from_name, message, comments_name, comments_message 

(in other words, flattening sublists)

the problem don't know in advance fields have in lists , i've not (for example, posts may miss comments section altogether)

tnx in advance help!


below quick trial. if big.list has names, can taken column names.

the inner do.call() flattens sub-list while outer 1 converts data frame.

lst1 <- list(from = list(id="12345678", name="gabriele"),message = "the quick fox",              comments = list(list(name="mickey", comment="hello world!"),                             list(name="donald", message="world...hello!"))) lst2 <- list(from = list(id="12345678", name="gabriele"),message = "the quick fox",              comments = list(list(name="mickey", comment="hello world!"))) lst3 <- list(from = list(id="12345678", name="gabriele"),message = "the quick fox")  df1 <- do.call(data.frame, do.call(c, lst1)) df2 <- do.call(data.frame, do.call(c, lst2)) df3 <- do.call(data.frame, do.call(c, lst3))  df1 #from.id from.name       message comments1.name comments1.comment comments2.name comments2.message #1 12345678  gabriele quick fox         mickey      hello world!         donald    world...hello!  df2 #from.id from.name       message comments.name comments.comment #1 12345678  gabriele quick fox        mickey     hello world!  df3 #from.id from.name       message #1 12345678  gabriele quick fox 

@ gabriele b

below clumsy working solution. others may post better one.

from <- list(id="12345678", name="gabriele") message <- "the quick fox" comments <- list(list(name="mickey", comment="hello world!"), list(name="donald", message="world...hello!")) big.list <- list(from = from, message = message, comments = comments) big.list1 <- list(from = from, message = message)  join <- function(lst) {   if(length(lst$comments) < 1) {     bnd <- data.frame(id = unlist(lst$from)[1], name = unlist(lst$from)[2], lst$message)     bnd$cmt.name <- "na"     bnd$comment <- "na"     bnd   } else {     bnd <- do.call(rbind, lapply(1:length(lst$comments), function(x) {       id <- unlist(lst$from)[1]       name <- unlist(lst$from)[2]       data.frame(id = id, name = name, lst$message)     }))     bnd <- cbind(bnd, do.call(rbind, lst$comments))     names(bnd) <- c("id", "name", "message", "cmt.name", "comment")     bnd   }   }  join(big.list) #id     name       message cmt.name        comment #id  12345678 gabriele quick fox   mickey   hello world! #id1 12345678 gabriele quick fox   donald world...hello!  join(big.list1) #id     name   lst.message cmt.name comment #id 12345678 gabriele quick fox       na      na 

Comments