0

I have a list of lists, like this:

tweetsdata <- list(politician1=list(meanlikes=2, meanretweet=1), politician2=list(meanlikes=4, meanretweet=3))

My desidered output is a data frame tweetsdata_df with 3 columns, like this:

|  POLITICIAN | MEANLIKES | MEANRETWEETS |

| ----------- | --------- | ------------ |

| Politician1 |     2     |      1       |

| Politician2 |     4     |      3       |

If I do a simple as.data.frame(tweetsdata) the output is a dataframe with 1 obs. of 4 variables (politician1.meanlikes, politician1.meanretweet, politician2.meanlikes, politician2.meanretweet)

If I try to create the dataframe manually I don't know how to access the right variable:

tweetsdata_df <- data.frame(POLITICIANS=names(tweetsdata), MEANLIKES=???, MEANRETWEETS=???)

What am I doing wrong?

IL DOGE
  • 31
  • 3

2 Answers2

1
library(tidyverse)

enframe(tweetsdata) |> unnest_wider(col="value")
Nir Graham
  • 2,567
  • 2
  • 6
  • 10
0

We can use purrr::map_dfr()

library(purrr)

map_dfr(tweetsdata, f = ~.x, .id = "politician").

# A tibble: 2 × 3
  politician  meanlikes meanretweet
  <chr>           <dbl>       <dbl>
1 politician1         2           1
2 politician2         4           3
GuedesBF
  • 8,409
  • 5
  • 19
  • 37