2017-04-01 75 views
-2

我一直在想辦法通過一堆包含吸光度數據的「csv」文件進行循環。文件名稱如下所示:Plate「x」0000000.csv - 「x」是板號,0表示由時間和觀察日期組成的唯一代碼。目標是運行我的源文件夾中的每個269個文件並一致地計算一些值。我爲計算完成了代碼。但是,我無法在所有文件中循環,應用計算,然後在新文件夾中將輸出保存爲新文件的csv文件:「x」000000.csv循環瀏覽文件並保存爲r文件中的新文件

wd = 'C:/Users/anadamsk/Documents/GRAD/DATA/Ecolog Plates' 

setwd(wd) 

dirs = dir(getwd()) 

for (i in 1:269){ 

    #absfiles = list.files(pattern = "*.csv") # list abs files 

plate <- read.csv("C:/Users/anadamsk/Documents/GRAD/DATA/Ecolog Plates/absfiles", ".csv", sep="/") 

    #for (j in 1:269) 

    #u_IDs = as.data.frame(strsplit(absfiles[j], '[.]')) 

    #unique_ID = as.character(u_IDs[1]) 

#plate <- u_IDs 

#plate <- fread('C:/Users/anadamsk/Documents/GRAD/DATA/Ecolog Plates/absfiles') 
#plate1 <-data.table(plate) 
#str(plate) 


colnames(plate)[1] <- "Well.ID" 
colnames(plate)[3] <- "Absorbance" 
#str(plate) 

setDT(plate)[, Well.ID:=Well.ID[1L] ,cumsum(Well.ID!='')] 
#str(plate)str() 

plate[,Well.ID:=as.character(Well.ID)][Well.ID == "BLK", Well.ID := "111BLK"] 

plate <- plate[,ID:=substr(Well.ID,4,6)] 

plate <- plate[,Group:=ID] 
plate <- plate[,Group:=as.factor(Group)] 
plate <- plate[Group == "BLK", Group := "0"] 
plate <- plate[,Group:=as.character(Group)] 
plate <- plate[,Group:=as.numeric(Group)] 
plate <- plate[Group > 0.9 & Group < 31.1, Group:= 1] 
plate <- plate[Group > 31.1 & Group < 62.1, Group := 2] 
plate <- plate[Group > 62.1 & Group < 93.1, Group := 3] 
#str(plate$Group) 




b1<-plate[Well %in% c("A1"),Absorbance] 
b2<-plate[Well %in% c("A5"),Absorbance] 
b3<-plate[Well %in% c("A9"),Absorbance] 

#group_1<-plate[,abs.c:=Absorbance-b1] 
#group_2<-plate[,abs.c:=Absorbance-b2] 
#group_3<-plate[,abs.c:=Absorbance-b3] 

plate[Group %in% c(1), abs.c:=Absorbance-b1] 
plate[Group %in% c(2), abs.c:=Absorbance-b2] 
plate[Group %in% c(3), abs.c:=Absorbance-b3] 

plate[Group %in% c(1), AWCD:=mean(abs.c)] 
plate[Group %in% c(2), AWCD:=mean(abs.c)] 
plate[Group %in% c(3), AWCD:=mean(abs.c)] 

plate[Group %in% c(1), Odi:=sum(abs.c,c(1))] 
plate[Group %in% c(2), Odi:=sum(abs.c,c(2))] 
plate[Group %in% c(3), Odi:=sum(abs.c,c(3))] 

plate[Group %in% c(1), Pi:=abs.c/Odi] 
plate[Group %in% c(2), Pi:=abs.c/Odi] 
plate[Group %in% c(3), Pi:=abs.c/Odi] 

plate[Group %in% c(1), sumpi:=Pi,c(1)*(log(Pi,c(1)))] 
plate[Group %in% c(2), sumpi:=Pi,c(2)*(log(Pi,c(2)))] 
plate[Group %in% c(3), sumpi:=Pi,c(3)*(log(Pi,c(3)))] 

plate[is.na(plate)] <- 0 

plate[Group %in% c(1), H:=sum(sumpi,c(1))] 
plate[Group %in% c(2), H:=sum(sumpi,c(2))] 
plate[Group %in% c(3), H:=sum(sumpi,c(3))] 


H. <- plate[!duplicated(H),] 
H. <- H.[!NA] 
platechar <-as.character(H.) 
#dir.create("./H_Values_Plates") 
file=H.(paste("./H_Values_Plates/",".csv",sep="/")) 
} 
+0

沒有數據,很難爲您提供可行的答案。問題:什麼是H?它在循環結束時看起來毫無用處。備註:通常,保存到csv文件的函數是write.csv。我會做'write.csv(H。,file = paste(「./ H_Values_Plates /」,「。csv」,sep =「/」))'' – xraynaud

+1

歡迎來到Stack Overflow。正如它所寫,目前還不清楚問題實際是什麼。而且,代碼行數太多。請將您的代碼編輯到最低限度,以便重現問題。參見[mcve]的指導。謝謝。 – Uwe

+0

感謝您的幫助。 H是顯示板的多樣性指數的變量。用你建議的write.csv結束它似乎是最好的選擇。 –

回答

0

paste('Plate',i,'000.csv', sep = '')在您的循環中使用i採取您需要的值應允許您循環。你似乎對你在循環中做的事有一個體面的把握。只是用它來獲取所有文件

+0

感謝您的幫助。幾乎在那裏。我的腳本現在不想結束循環。 –

相關問題