如果存在一些重复,则有条件运行



我有以这种方式命名的文件列表:

Myexpdate1_R1.txt        
Myexpdate1_R2.txt     
Myexpdate1_R3.txt       
Myexpdate2_R1.txt     
Myexpdate2_R2.txt       
Myexpdate2_R3.txt      

如何要求r运行管道,仅用于为三个重复而不是针对其他重复的实验?换句话说,如果情况为以下:

Myexpdate1_R2.txt     
Myexpdate1_R3.txt       
Myexpdate2_R1.txt     
Myexpdate2_R2.txt       
Myexpdate2_R3.txt    

该代码不会为myExpdate1运行,因为MyExpdate1_r1.txt不可用,但是它将用于myexpdate1_r2.txt,因为所有三个重复都可以使用。我尝试通过将包含模式*R[1-3].txt的文件的list.files()的长度除以3,以便在返回整数时运行,但不可行,但不幸的是,我在整数正确地识别整数时遇到了麻烦。

假设您从文件名flist的列表开始这应该给你一个d.f.如果您在一个实验中错过了" R",则使用execute列设置为0,则否则1。例如:

flist <- c("Myexpdate1_R1.txt", "Myexpdate1_R2.txt", "Myexpdate1_R3.txt",       
           "Myexpdate2_R1.txt", "Myexpdate2_R2.txt") 
library(dplyr)
library(stringr)
library(tibble)
flist <- c("Myexpdate1_R1.txt", "Myexpdate1_R2.txt", "Myexpdate1_R3.txt",       
           "Myexpdate2_R1.txt", "Myexpdate2_R2.txt") 
exec <-  flist %>% 
  str_split_fixed("_",2) %>% 
  as_tibble() %>% 
  mutate(replicas = str_split_fixed(V2, ".txt",2)[,1]) %>% 
  group_by(V1) %>% 
  dplyr::summarise(execute = ifelse (n() == 3, 1, 0))
> exec
# A tibble: 2 × 2
  Experiment execute
       <chr>   <dbl>
1 Myexpdate1       1
2 Myexpdate2       0

然后,您可以使用exec来决定是否运行模拟。例如,使用简单的for循环:

names(exec)[1] <- "Experiment"
for (exp in seq(along = exec$Experiment)){
  if (exec[exp,]$execute == 1){
    message("Experiment:", exec[exp,]$Experiment,"--> OK, RUN")
    print("DOING SOMETHING")
  } else{
    message("Experiment:", exec[exp,]$Experiment,"--> FAIL")
    print("DOING NOTHING")
    }
}

实验:myexpdate1->好,运行
"做某事"
实验:myexpdate2->失败
"什么都不做"

最新更新