Oliver
Oliver

Reputation: 8572

Data.frame from list of rows

A rather simple answer.

Given a named named json-like list of data.frame rows, how would one transform this into a proper data.frame in a concise manner while keeping the column classes and row names intact.

df_list <- lapply(1:10, function(x)list(a = 1, b = 'hello', c = 3 - 1i))
names(df_list) <- LETTERS[1:10]

desired result

data.frame(a = rep(1, 10), b = rep('hello', 10), c = rep(3 - 1i, 10))

Upvotes: 1

Views: 44

Answers (1)

akrun
akrun

Reputation: 887128

An option with unnest_wider

library(dplyr)
library(tidyr)
tibble(col1 = df_list) %>% 
      unnest_wider(c(col1))

Or with bind_rows

bind_rows(df_list)

Or with rbindlist from data.table

library(data.table)
rbindlist(df_list)

Upvotes: 1

Related Questions