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?

0

2 Answers 2

1
library(tidyverse)

enframe(tweetsdata) |> unnest_wider(col="value")
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.