2017-03-05 20 views
0

我以這種方式命名的文件的列表:條件運行,如果一些重複存在

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爲了運行,如果它返回一個整數,而不是否則運行,但不幸的是我由R.

感謝整數的適當識別過程中經歷的麻煩提前

+1

尚不清楚。也許'sapply(split(files,sub(「_。*」,「」,files)),function(x)length(unique(x)))== 3'或者可以是'lapply(split(files,sub (x,read.table,stringsAsFactors = FALSE))' – akrun

+1

你是不是指「如果有一些*(*。*」,「」,files)),函數(x)if(length(unique(x))lapply *文件**存在「,在標題中? – lbusett

回答

1

假設你從一個文件名列表開始flist, 這應該給你一個df如果在一個實驗中錯過了「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 - >確定,RUN
「有所作爲」
實驗:Myexpdate2 - > FAIL
「無爲」

+0

剛剛編輯借用@akrun較短的「模式檢查」 – lbusett

+0

嗨LoBu。我試圖運行你建議給我的那段代碼,但是如果我想添加一個新的條件,也就是說,管道只能在Myexpdate2上運行,而我有三個複製,而不是在Myexpdate1上,所以我必須修改代碼寫的嗎? – Bfu38

+0

嗨。對不起,我不明白。你想補充的進一步條件是什麼? – lbusett