将列表列表转换为R中的数据框:Tidyverse-way [英] Converting a list of lists to a dataframe in R: The Tidyverse-way

查看:86
本文介绍了将列表列表转换为R中的数据框:Tidyverse-way的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索将列表列表转换为R中的数据框的Tidyverse方法。

I am searching for the Tidyverse way of converting a list of lists to a dataframe in R.

# Create a list of lists:
a <- seq(1,10,1)
b <- seq(1,20,2)

# Function to calculate the sum 
# (just an example, I am aware of the base R sum())

sum_test <- function(a=a, b=b){
  sum <- a+b
  df <- cbind(a,b,sum)
  return(df)
}

list_of_lists <- purrr::map2(a,b,sum_test)

创建列表列表数据框的非tidyverse方法:

Non-tidyverse way to create a dataframe of the list of lists:

df <- as.data.frame(do.call(rbind, list_of_lists))

问题

如何将列表转换为

推荐答案

您可以使用

purrr::map_df(list_of_lists, tibble::as_tibble)

# A tibble: 10 x 3
#      a     b   sum
#   <dbl> <dbl> <dbl>
# 1     1     1     2
# 2     2     3     5
# 3     3     5     8
# 4     4     7    11
# 5     5     9    14
# 6     6    11    17
# 7     7    13    20
# 8     8    15    23
# 9     9    17    26
#10    10    19    29

这篇关于将列表列表转换为R中的数据框:Tidyverse-way的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆