R purrr 从列表中提取多个项目并转换为数据框

R purrr extracting multiple items from a list and converting to a data frame

我目前正在学习 purrr R。我有代码可以执行以下操作

下面是一个例子,我认为我已经完成了大约 90%。我想要做的就是将学校的名称作为列添加到数据框中,以便之后能够绘制图表。谁能帮忙?一旦 bind_rows() 命令为 运行

,下面的方法就会丢失名称
library(lavaan)
library(tidyverse)

# function pulls the mean, sd, range, kurtosis and skew
get_stats <- function(x){

  row_names <- rownames(x)
  mydf_temp <- x %>% 
    dplyr::select(mean, sd, range, kurtosis, skew) %>% 
    mutate_if(is.numeric, round, digits=2) %>% 
    filter(complete.cases(.))

  mydf_temp
}

# Generate the data for the reproducible example
mydf <- HolzingerSwineford1939 %>% 
  select(school, starts_with("x")) %>% 
  psych::describeBy(., group=.$school, digits = 2) 


# Gets the summary statistics per school
stats_summ <- mydf %>% 
  map(get_stats) %>% 
  bind_rows()

我们可以使用 bind_rows

中的 .id 参数
mydf %>%
     map(get_stats) %>%
     bind_rows(., .id = 'group')

使用具有 iris 数据集的可重现示例

mydf <- iris %>%   
           psych::describeBy(., group=.$Species, digits = 2)