-1

I have a list such as: (only with 158 sublists)

> adjlist
$innocent
$innocent$competence
[1] 4.1

$innocent$desirable
[1] 4.68

$innocent$masculinity
[1] 2.69

$innocent$warmth
[1] 5.26


$enthusiastic
$enthusiastic$competence
[1] 5.21

$enthusiastic$desirable
[1] 5.81

$enthusiastic$masculinity
[1] 3.93

$enthusiastic$warmth
[1] 5.64

And want to convert it to a data frame such as: (I created this one manually)

             competence masculinity desirable warmth
innocent           4.10        2.69      4.68   5.26
enthusiastic       5.21        3.93      5.81   5.64

Thanks in advance!

  • Please show the dput of the example – akrun Nov 22 '16 at 16:45
  • Try `library(reshape2); acast(melt(adjlist), L1~L2, value.var = "value")` – akrun Nov 22 '16 at 16:50
  • Thank you so much!! I don't understand "Please show the dput of the example", sorry! What do you mean by that? – Daniel Schulz Nov 22 '16 at 16:56
  • Nevermind, I created the dataframe and tested. It worked for me with the example you showed – akrun Nov 22 '16 at 16:58
  • @DanielSchulz it meant: please show us the output of dput(adjlist). Take a look at this [link](http://stackoverflow.com/a/5963610/6167055) – Tomás Barcellos Nov 22 '16 at 17:00
  • Yes it worked for me to, therefore the "Thank you" Sorry if I didn't make that clear. Have a nice day you two – Daniel Schulz Nov 22 '16 at 17:09
  • 1
    This was constructed by `adjlist = list(innocent=list(competence=4.1, desirable=4.68, masculinity=2.69, warmth=5.26), enthusiastic=list(competence=5.21, desirable=5.81, masculinity=3.93, warmth=5.64))` or similar? – tkerwin Nov 22 '16 at 17:39

1 Answers1

4

If I've understood correctly, this should do what you want without any extra libraries.

res <- do.call(rbind, Map(data.frame, adjlist))
class(res)      # "data.frame"
rownames(res)   # "innocent", "enthusiastic"
colnames(res)   # "competence", "desirable", "masculinity", "warmth"

Map(data.frame, adjlist) will apply data.frame to each element of adjlist and return a list of data frames, each with one row. do.call(rbind, ...) will take this output and reduce it to a single data frame. By default, names should be preserved.

d2b
  • 76
  • 2
  • 6