简体   繁体   English

dplyr:使用列表的对象名称创建列

[英]dplyr: create column with list's objects' names

I have two data frames, df1_09 and df1_10 , that I put together in a list, list_of_df :我有两个数据框df1_09df1_10 ,我将它们放在一个列表list_of_df中:

library(tidyverse)

df1_09 <- data.frame(
  index = c(1, 1, 2, 2),
  first_column = c(1, 2, 3, 4),
  second_column = c(5, 6, 7, 8)
)

df1_10 <- data.frame(
  index = c(1, 1, 2, 2),
  first_column = c(4, 2, 3, 1),
  second_column = c(8, 6, 7, 5)
)

list_of_df <- list(df1_09, df1_10)

Now I create a function that takes in one of the original data frames, df1_09 , groups the values according to the index variable, and calculates the means for each column.现在我创建一个 function ,它接收原始数据帧之一df1_09 ,根据index变量对值进行分组,并计算每列的平均值。 The function also creates an additional column called type with the name of the data frame. function 还创建了一个名为type的附加列,其中包含数据框的名称。

output_mean <- function(subset, name) {
  subset %>%
    group_by(index) %>%
    summarize(across(c(first_column, second_column), ~ mean(.x, na.rm = TRUE))) %>%
    mutate(type = name) %>%
    print()
}

output_mean(df1_09, "df1_09")
#> # A tibble: 2 x 4
#>   index first_column second_column type  
#>   <dbl>        <dbl>         <dbl> <chr> 
#> 1     1          1.5           5.5 df1_09
#> 2     2          3.5           7.5 df1_09

Now what I would like to do, is to loop through the list list_of_df and get the same result.现在我想做的是list_of_df获得相同的结果。 I have tried the following:我尝试了以下方法:

for (i in list_of_df) {
 output_mean(i, "i")
}
#> # A tibble: 2 x 4
#>   index first_column second_column type 
#>   <dbl>        <dbl>         <dbl> <chr>
#> 1     1          1.5           5.5 i    
#> 2     2          3.5           7.5 i    
#> # A tibble: 2 x 4
#>   index first_column second_column type 
#>   <dbl>        <dbl>         <dbl> <chr>
#> 1     1            3             7 i    
#> 2     2            2             6 i

The problem is that I don't know how to get to the names of each object in the list.问题是我不知道如何获取列表中每个 object 的名称。 Have the names being deleted with the creation of the list?创建列表时是否删除了名称?

Created on 2021-05-02 by the reprex package (v2.0.0)reprex package (v2.0.0) 于 2021 年 5 月 2 日创建

We can create a named list , loop over the names of the list , extract the list element with [[ while passing the name argument as i我们可以创建一个命名list ,遍历listnames ,使用[[提取list元素,同时将名称参数作为i传递

list_of_df <- dplyr::lst(df1_09, df1_10)
for (i in names(list_of_df)) {
   output_mean(list_of_df[[i]], i)
  }

-output -输出

# A tibble: 2 x 4
#  index first_column second_column type  
#* <dbl>        <dbl>         <dbl> <chr> 
#1     1          1.5           5.5 df1_09
#2     2          3.5           7.5 df1_09
# A tibble: 2 x 4
#  index first_column second_column type  
#* <dbl>        <dbl>         <dbl> <chr> 
#1     1            3             7 df1_10
#2     2            2             6 df1_10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM