如何在结果输出列名称中包含提供给函数的输入文件名

How to include input file name provided to a function in the resultant output column name

我不熟悉在列表中使用函数。

我编写了一个函数来对一组值执行统计测试。我将值集作为列表导入函数作为输入和 运行 程序。但是我观察到输出文件具有相似的输出列名称。我们可以 replace/include 输出文件列名中的输入文件名吗?

这里附上示例代码。

library(nycflights13)
library(tidyverse)

## Sample data ##
FlightOriginDestination <- nycflights13::flights %>% 
  dplyr::select(flight, origin, dest) %>% 
  dplyr::distinct()

Dest <- FlightOriginDestination %>% 
  dplyr::select(dest) %>% 
  dplyr::distinct()

## Mulptiple input files ##
List1 <- Dest %>% 
  slice_sample(n = 5) %>% 
  dplyr::distinct()

List2 <- Dest %>% 
  slice_sample(n = 5) %>% 
  dplyr::distinct()

List3 <- Dest %>% 
  slice_sample(n = 5) %>% 
  dplyr::distinct()

## Combining the input files
Destination <- list(
  List1,
  List2,
  List3
)

## Function to perform a basic quantitative representation
Function1 <- function(Data){
  Data %>% 
  dplyr::left_join(FlightOriginDestination) %>% 
  dplyr::distinct() %>% 
  dplyr::count(flight, name = "Data")
}

## applying the function on the list
map(Destination, Function1) %>% 
  reduce(full_join, by = "flight") %>% 
  mutate_all(~replace(., is.na(.), 0))

下面是输出文件的header。我们可以 replace/include Data.x, Data.y 和具有输入文件名的数据 (List 1, List2, List3).

## Output ##

Joining, by = "dest"
Joining, by = "dest"
Joining, by = "dest"
# A tibble: 1,396 x 4
flight Data.x Data.y  Data
<dbl>  <dbl>  <dbl> <dbl>
1      4      1      0     0
2      9      1      0     0
3     18      1      0     0
4     27      1      0     0
5     28      1      0     0
6     31      1      0     0
7     41      1      0     0
8     43      1      0     0
9     45      1      0     0
10     52      1      0     0
# ... with 1,386 more rows

我是 Whosebug 的新手。如果问题的呈现很复杂,请告诉我。

我把Destination做成了一个命名列表。然后在函数中使用 rename_with() 来重命名除第一列以外的所有列,名称来自命名列表

Destination <- list(
    List1 = List1,
    List2 = List2,
    List3 = List3
)

## Function to perform a basic quantitative representation
Function1 <- function(Data){
    Data %>% 
        dplyr::left_join(FlightOriginDestination) %>% 
        dplyr::distinct() %>% 
        dplyr::count(flight, name = "Data")
}

## applying the function on the list
map(Destination, Function1) %>% 
    reduce(full_join, by = "flight") %>% 
    mutate_all(~replace(., is.na(.), 0)) %>%
    rename_with(function(x) names(Destination), .cols = -1)
    ```

Output looks like this:
 flight List1 List2 List3
    <dbl> <dbl> <dbl> <dbl>
 1      1     1     0     0
 2      5     1     0     0
 3      7     1     0     0
 4     10     1     0     0
 5     11     1     0     0
 6     12     1     0     0
 7     15     1     0     0