0

What is the optimum way for creating a data frame from a nested list.

#installing package
installed.packages("qdap")
#loading qdap package
library("qdap")

# Finding synonyms from the below function which gives a list.
data <- synonyms("power")

# Converting list to dataframe
plyr::ldply(data, cbind)

t(plyr::ldply(data, rbind))

At last, I would like to create multiple data frame. from the output of:

synonyms(c("taste","company","power"))

list output looks alike using dput()

structure(list(power.def_1 = c("ability", "capability", "capacity", 
"competence", "competency", "faculty", "potential"), power.def_2 = c("brawn", 
"energy", "force", "forcefulness", "intensity", "might", "muscle", 
"potency", "strength", "vigour", "weight"), power.def_3 = c("ascendancy", 
"authority", "bottom", "command", "control", "dominance", "domination", 
"dominion", "influence", "mastery", "rule", "sovereignty", "supremacy", 
"sway"), power.def_4 = c("authority", "authorization", "licence", 
"prerogative", "privilege", "right", "warrant")), .Names = c("power.def_1", 
"power.def_2", "power.def_3", "power.def_4"))
15
  • 3
    Please post list data using dput(). Commented Jun 27, 2018 at 8:25
  • everyone that wants to help you needs to install it, which puts me off from answering questions like that. Commented Jun 27, 2018 at 8:32
  • ok so let me share the structure with you... Commented Jun 27, 2018 at 8:33
  • @PoGibas i've included the list structure for your reference. Thanks in advance for your help. Commented Jun 27, 2018 at 8:40
  • 1
    Ok. probably not. Would be best if you were clearer about input and expected output. Commented Jun 27, 2018 at 10:18

1 Answer 1

1

Would it work like this ?

lapply(l, function(li)
  data.frame(lapply(li,'length<-',max(lengths(li)))))

# [[1]]
# power.def_1  power.def_2 power.def_3   power.def_4
# 1      ability        brawn  ascendancy     authority
# 2   capability       energy   authority authorization
# 3     capacity        force      bottom       licence
# 4   competence forcefulness     command   prerogative
# 5   competency    intensity     control     privilege
# 6      faculty        might   dominance         right
# 7    potential       muscle  domination       warrant
# 8         <NA>      potency    dominion          <NA>
# 9         <NA>     strength   influence          <NA>
# 10        <NA>       vigour     mastery          <NA>
# 11        <NA>       weight        rule          <NA>
# 12        <NA>         <NA> sovereignty          <NA>
# 13        <NA>         <NA>   supremacy          <NA>
# 14        <NA>         <NA>        sway          <NA>

...

More compact using purrr :

library(purrr)
map(l,~map_dfc(.,`length<-`,max(lengths(.))))

data

l1 <- structure(list(power.def_1 = c("ability", "capability", "capacity", 
                                    "competence", "competency", "faculty", "potential"), power.def_2 = c("brawn", 
                                                                                                         "energy", "force", "forcefulness", "intensity", "might", "muscle", 
                                                                                                         "potency", "strength", "vigour", "weight"), power.def_3 = c("ascendancy", 
                                                                                                                                                                     "authority", "bottom", "command", "control", "dominance", "domination", 
                                                                                                                                                                     "dominion", "influence", "mastery", "rule", "sovereignty", "supremacy", 
                                                                                                                                                                     "sway"), power.def_4 = c("authority", "authorization", "licence", 
                                                                                                                                                                                              "prerogative", "privilege", "right", "warrant")), .Names = c("power.def_1", 
                                                                                                                                                                                                                                                           "power.def_2", "power.def_3", "power.def_4"))

l <- list(l1,l1)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help here, however for every sublist i would like to create a different data frame instead of rowbind. sorry if i was not clear at the beginning.however got some hint from your solution.
so if you just remove the do.call(rbind, you get what you want ?
correct, and running the nested list in a loop by assigning n no of dataframes
I updated my answer to remove the row binding, I don't understand your last comment though and cannot install the qdap package. If you need futher help please elaborate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.