loop for applying script to sub-folders and name results accordingly --R

久未见 提交于 2019-12-24 13:56:57

问题


I have a script to get corssproduct of test-results.csv file against any exsiting train .csv file in one folder

Edited -- some corrections added

mytest <- function(data) {
data01 <- as.matrix(read.csv(file = "test/test-results.csv", sep = ",", header=FALSE))
data02 <- as.matrix(read.csv(file = data, sep = ",", header=FALSE))
test <- list()
test01<- list()
test02<- list()
i<- 1
while (i <= 25){
    test01[[i]] <- c(data01[i,  ])
    test02[[i]] <- c(data02[i,  ])
    test[[i]]<- crossprod(test01[[i]],test02[[i]])
    i <- i+1
}
return(mytest)
}
result <- lapply(list.files(pattern='^tain.*\\.csv', recursive=TRUE, full.names = TRUE),test)
for (i in seq(length(result))) {
 write.csv(do.call(cbind,result),'result.csv', row.names = FALSE) 
}

I have few folders with same structure

Main|experiment 01|test|test-results.csv, train01.csv, train02.csv, train03.csv
Main|experiment 02|test|test-results.csv, train01.csv, train02.csv

I now want to create a script if possible to apply the same script to all of those folders and return the results (e.g. results-experiment01.csv) instead of applying them 100 times

sorry if this looks horrible ... new in R


回答1:


lets say with just plain dir or list.files call with recursive = TRUE you get a file list as character vector (like dirlist in example below)

> dirlist
[1] "Main/experiment 01/test/a.csv"  "Main/experiment 02/test/b.csv"  "Main/experiment 02/test/d.txt"  "Main/experiment 02/dummy/a.csv"
[5] "temp1.csv"                      "Main/temp2.csv"  

You can subset this vector using regular expressions as follows

> csvfiles <- dirlist[grepl('.*(experiment [0-9]+)/test/.*\\.csv', dirlist)]
> csvfiles
[1] "Main/experiment 01/test/a.csv" "Main/experiment 02/test/b.csv"

Then you can get result names using regex again on this.

> resultsnames <-  paste('result_', gsub('.*(experiment [0-9]+)/test/.*\\.csv','\\1', csvfiles), sep="")
> resultsnames
[1] "result_experiment 01" "result_experiment 02"


来源:https://stackoverflow.com/questions/15095196/loop-for-applying-script-to-sub-folders-and-name-results-accordingly-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!