循环浏览文件并在r中保存为新文件

时间:2017-04-01 17:51:26

标签: r loops data.table

我一直试图想办法绕过一堆" csv"包含吸光度数据的文件。文件名读作:Plate" x" 0000000.csv - " x"是一个板号,0表示由观察时间和日期组成的唯一代码。目标是遍历源文件夹中的每个269文件并一致地计算一些值。我已经为计算完成了代码。但是,我无法循环遍历所有文件,应用计算,然后将输出保存为新文件,将输出作为csv文件放在新文件夹中:Plate" x" 000000.csv

 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="/"))
}

1 个答案:

答案 0 :(得分:0)

循环中的{p> paste('Plate',i,'000.csv', sep = '') i获取您需要的值应允许您循环播放。你似乎对你在循环中所做的事情有了一个很好的把握。只需使用它来获取所有文件