Question

I would like to execute anova on multiple datasets stored in my working directory. I have come up so far with:

files <- list.files(pattern = ".csv")
for (i in seq_along(files)) {
    mydataset.i <- files[i]
    AnovaModel.1 <- aov(DES ~ DOSE, data=mydataset.i)
    summary(AnovaModel.1)
} 

As you can see I am very new to loops and cannot make this work. I also understand that I need to add a code to append all summary outputs in one file. I would appreciate any help you can provide to guide to the working loop that can execute anovas on multiple .csv files in the directory (same headers) and produce outputs for the record.

Was it helpful?

Solution

you might want to use list.files with full.names = TRUE in case you are not on the same path.

files <- list.files("path_to_my_dir", pattern="*.csv", full.names = T)
# use lapply to loop over all files
out <- lapply(1:length(files), function(idx) {
    # read the file
    this.data <- read.csv(files[idx], header = TRUE) # choose TRUE/FALSE accordingly
    aov.mod <- aov(DES ~ DOSE, data = this.data)
    # if you want just the summary as object of summary.aov class
    summary(aov.mod)
    # if you require it as a matrix, comment the previous line and uncomment the one below
    # as.matrix(summary(aov.mod)[[1]])
})
head(out)

This should give you a list with each entry of the list having a summary matrix in the same order as the input file list.

OTHER TIPS

Your error is that your loop is not loading your data. Your list of file names is in "files" then you start moving through that list and set mydataset.i equal to the name of the file that matches your itterator i... but then you try to run aov on the file name that is stored in mydataset.i!

The command you are looking for to redirect your output to a file is sink. Consider the following:

sink("FileOfResults.txt") #starting the redirect to the file
files <- list.files("path_to_my_dir", pattern="*.csv", full.names = T) #using the fuller code from Arun
for (i in seq_along(files)){
   mydataset.i <- files[i]
   mydataset.d <- read.csv(mydataset.i) #this line is new
   AnovaModel.1 <- aov(DES ~ DOSE, data=mydataset.d) #this line is modified
   print(summary(AnovaModel.1))
} 
sink() #ending the redirect to the file

I prefer this approach to Arun's because the results are stored directly to the file without jumping through a list and then having to figure out how to store the list to a file in a readable fashion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top