我可以使用 group_map 或 group_walk 来迭代导出结果吗?

Can I use group_map or group_walk to iteratively export results?

我想使用 group_walk() 或 group_map() 作为导入成批 .csv 文件的替代方法来迭代处理主比较列表。

我想输入一个如下所示的数据集:

测试分析 Var1 Var2 频率
检测 1 否定 否定 19
检测 1 否定 位置 5
检测 1 位置 否定 8
检测 1 位置 位置 141
检测 2 否定 否定 25
检测 2 否定 位置 6
检测 2 位置 否定 17
检测 2 位置 位置 33
检测 3 否定 否定 99
检测 3 否定 位置 20
检测 3 位置 否定 5
检测 3 位置 位置 105

我想使用函数 epi_analysis 并为每个测试分析(在本例中为 Assay1、Assay2 和 Assay3)导出一个 csv。到目前为止我有:

#Make export directory
check_create_dir <- function(the_dir) {
  if (!dir.exists(the_dir)) {
    dir.create(the_dir, recursive = TRUE) } #Creates a directory if it doesn't already exist
}

the_dir_ex <- "data_generated/epidata" #Name the new desired directory

check_create_dir(the_dir_ex) #Make the directory if it doesn't already exist

#Make function for the series of analyses
epi_analysis <- function(.x, the_dir){
  #Clean data
  dat2 <- .x  %>%
    select(c(Var1, Var2, Freq)) %>%
    pivot_wider(Var1, names_from = Var2, values_from = Freq) %>%
    remove_rownames %>% 
    column_to_rownames( var = "Var1") %>% 
    as.matrix() 
  
  #Run tests
  rval <- epi.tests(dat2, conf.level = 0.95)
  rkappa<-epi.kappa(dat2)
  gwet <- gwet.ac1.table(dat2)
  kappa2 <- kappa2.table(dat2)
  
  #Export results
  hd <- c('sensitivity', 'specificity', 'pfp', 'pfn', 'kappa', 'gwet', 'pabak')
  ests <- c(round(rval$elements$sensitivity$est, digits = 3), 
            round(rval$elements$specificity$est, digits = 3), 
            round(rval$element$pfp$est, digits = 3), 
            round(rval$element$pfn$est, digits = 3), 
            round(kappa2$coeff.val, digits = 3), 
            round(gwet$coeff.val, digits = 3), 
            round(rkappa$pabak$est, digits = 3))
  cis <- c(paste(round(rval$elements$sensitivity$lower, digits = 3), round(rval$elements$sensitivity$upper, digits = 3), sep = ","), 
           paste(round(rval$elements$specificity$lower, digits = 3), round(rval$elements$specificity$upper, digits = 3), sep = ","),
           paste(round(rval$element$pfp$lower, digits = 3), round(rval$element$pfp$upper, digits = 3), sep = ","),  
           paste(round(rval$element$pfn$lower, digits = 3), round(rval$element$pfn$upper, digits = 3), sep = ","), 
           kappa2$coeff.ci, 
           gwet$coeff.ci, 
           paste(round(rkappa$pabak$lower, digits = 3), round(rkappa$pabak$lower, digits = 3), sep = ","))
  
  df <- data.frame(hd, ests, cis)
  
  write.csv(df, 
            file = paste0(the_dir, "/", basename(.x$TestAssay)),
            na = "999.99", 
            row.names = FALSE)
  
}


#Use group_map or group_walk to iterate through the different assays in the dataset.

data <- read_csv("data_raw/EpiTest.csv") %>%
  group_by(TestAssay)%>%
  group_map(~ epi_analysis)

但是我的epidata文件夹里没有csvs。欢迎任何 suggestions/corrections。

您需要在 group_map 中调用您的函数。该函数还需要两个参数,因此也传递 the_dir_ex

使用这个功能-

library(tidyverse)
library(epiR)
library(irrCAC)


epi_analysis <- function(.x, the_dir){
dat2 <- .x  %>%
  select(c(Var1, Var2, Freq)) %>%
  pivot_wider(Var1, names_from = Var2, values_from = Freq) %>%
  remove_rownames %>% 
  column_to_rownames( var = "Var1") %>% 
  as.matrix() 

#Run tests
rval <- epi.tests(dat2, conf.level = 0.95)
rkappa<-epi.kappa(dat2)
gwet <- gwet.ac1.table(dat2)
kappa2 <- kappa2.table(dat2)

#Export results
hd <- c('sensitivity', 'specificity', 'pfp', 'pfn', 'kappa', 'gwet', 'pabak')
ests <- c(round(rval$elements$sensitivity$est, digits = 3), 
          round(rval$elements$specificity$est, digits = 3), 
          round(rval$element$pfp$est, digits = 3), 
          round(rval$element$pfn$est, digits = 3), 
          round(kappa2$coeff.val, digits = 3), 
          round(gwet$coeff.val, digits = 3), 
          round(rkappa$pabak$est, digits = 3))
cis <- c(paste(round(rval$elements$sensitivity$lower, digits = 3), round(rval$elements$sensitivity$upper, digits = 3), sep = ","), 
         paste(round(rval$elements$specificity$lower, digits = 3), round(rval$elements$specificity$upper, digits = 3), sep = ","),
         paste(round(rval$element$pfp$lower, digits = 3), round(rval$element$pfp$upper, digits = 3), sep = ","),  
         paste(round(rval$element$pfn$lower, digits = 3), round(rval$element$pfn$upper, digits = 3), sep = ","), 
         kappa2$coeff.ci, 
         gwet$coeff.ci, 
         paste(round(rkappa$pabak$lower, digits = 3), round(rkappa$pabak$lower, digits = 3), sep = ","))

df <- data.frame(hd, ests, cis)

write.csv(df, 
          file = sprintf('%s/%s.csv', the_dir, .x$TestAssay[1]),
          na = "999.99", 
          row.names = FALSE)

}

并用 -

调用它
read_csv("data_raw/EpiTest.csv") %>%
  group_by(TestAssay)%>%
  group_map(~epi_analysis(., the_dir_ex), .keep = TRUE)

我们可以使用

library(dplyr)
library(readr)
library(purrr)
read_csv("data_raw/EpiTest.csv") %>%
   group_split(TestAssay) %>%
   map(~ epi_analysis(.x, the_dir_ex))