Reputation: 533
I'm unsure wether this topic has been adressed before or not but I've only been able to find similar questions using the pattern =
character.
I'm creating different percentage tables based on files with the following pattern name: "accuracycollar4136*_4136*.0.*.csv"
- were * is a whole number (e.g. 1, 2, 3 etc.). All of these files are in the same folder.
I want to create a for
loop that will write in csv format every single percentage table based on the "accuracycollar4136*_4136*.0.*.csv"
files.
Here's my code when writing in csv a percentage table for one single "accuracycollar4136*_4136*.0.*.csv"
file, here "accuracycollar41361_41365.0.7.csv"
as an example:
setwd("C:/Users/Juan/Desktop/KNN/41361_by_41365")
files = as.list(list.files(path = "C:/Users/Juan/Desktop/KNN/41361_by_41365"))
miss_class = lapply(files, function(x){
data = read.csv("accuracycollar41361_41365.0.7.csv",header=T)
miss<-prop.table(table(data$observed,data$predicted),margin=1)
})
write.csv(miss,file="classification_table_41361_by_41365.0.7.csv")
With output:
Grazing Head-up Unknown Vigilance
Grazing 0.291666667 0.583333333 0.020833333 0.104166667
Grooming 0.750000000 0.000000000 0.000000000 0.250000000
Head-up 0.331168831 0.538961039 0.006493506 0.123376623
Moving 0.000000000 0.923076923 0.000000000 0.076923077
Unknown 0.250000000 0.750000000 0.000000000 0.000000000
Vigilance 0.444444444 0.333333333 0.000000000 0.222222222
Now, how can I automat this same process with a for
loop for every file matching the name pattern "accuracycollar4136*_4136*.0.*.csv"
?
I know there's the pattern=
character but that will not work here where parts of the pattern are variable.
Any input is appreciated!
P.S. (Update): I've been trying something like this based on previous scripts but I get no output. I'm I on the right track?
setwd("C:/Users/Juan/Desktop/KNN/41361_by_41365")
f <- list.files(full.names = F, pattern = "accuracycollar413")
dfs <- list()
for(i in 1:length(f))
miss_class = lapply(files, function(x){
data = read.csv(f,header = TRUE)
miss<-prop.table(table(data$observed,data$predicted),margin=1)
write.csv(miss,file="classification_table",i,"csv",sep=".")
})
Upvotes: 0
Views: 797
Reputation: 3369
You could try something like this.
setwd("C:/Users/Juan/Desktop/KNN/41361_by_41365")
files <- list.files(path = "C:/Users/Juan/Desktop/KNN/41361_by_41365")
files <- files[which(regexpr("accuracycollar4136\\d_4136\\d.0.\\d.csv", files) == 1)]
for(i in files){
data <- read.csv(i, header=TRUE)
miss <- prop.table(table(data$observed, data$predicted), margin=1)
write.csv(miss, file = sub("accuracycollar", "classification_table_", i))
}
Upvotes: 1