user3525495
user3525495

Reputation: 1

How to apply a loop to my R script

I will preface my question that I am new to R and never programmed in any other language. I have been reading the topics on this site about looping and the apply functions. This question is likely considered a duplicate, but I am not finding a solution to my needs. I have a simple script that I would like to apply to all files in a folder. What I would like to have happen is that the script would be applied to each file in a folder and provide an output file with the results from each file. I originally thought this would be a batch procedure, but from all of my searching it looks like a loop is more appropriate.

I have tried to implement the methodology from applying R script prepared for single file to multiple files in the directory.

This is what I have currently:

    library("moments")
    library("lattice")
    listfile<- dir()  ## Creates a list of the files in the folder
    listfile<- c("raster01_table.csv","Test.csv", "Test2.csv")
    lst<-vector("list", length(listfile)) #don't know what this is doing
    names(lst) <-listfile # or this
    lst[listfile[i]]  ## or this

    #The generalized process to calculate the 4 moments
    for (i in 1:length(listfile)) {
    tmp.df <- read.csv(listfile[i], header = T)  
    names(tmp.df)[1]<- c("Value")  #DEFINES COLUMN NAMES
    names(tmp.df)[2]<- c("Count")   #DEFINES COLUMN NAMES
    ##CALCULATES THE FOUR MOMENTS + 1 (IS THE SECOND MOMENT SD OR VAR?)
    objMean <-mean(tmp.df$Value)  #calculates the mean of Value column 
    objSD <-sd(tmp.df$Value)  #calculates the standard deviation of Value column
    objVar <-var(tmp.df$Value)  #Calculates the variance of Value column
    objSkewness <-skewness(tmp.df$Value, na.rm = FALSE)  #calculate the skewness of Value Column
    objKurtosis <-kurtosis(tmp.df$Value, na.rm = FALSE)  #Calculate the kurtosis of Value column
     }

The problem with this code is that it only seems to run on the first file in the list and thus does not process the remaining files in the folder.

Upvotes: 0

Views: 207

Answers (1)

mherradora
mherradora

Reputation: 21

I've been working in my job with this kind of issues. You have to do some little changes in the dir() command. Here is the new script

library("moments")
library("lattice")

listfile <- dir(getwd(),        #This call the working directory
            pattern = "csv",    #This define the kind of files that you need to process
            recursive = TRUE,   #This use the files in the subdirectories
            all.files = TRUE,   #This indicate that you will process all the files
            full.names = TRUE)  #This load the full path of a file

#The generalized process to calculate the 4 moments
for (i in 1:length(listfile)) {
  tmp.df <- read.csv(listfile[i], header = T)  
  names(tmp.df)[1]<- c("Value")  #DEFINES COLUMN NAMES
  names(tmp.df)[2]<- c("Count")   #DEFINES COLUMN NAMES
  ##CALCULATES THE FOUR MOMENTS + 1 (IS THE SECOND MOMENT SD OR VAR?)
  objMean <-mean(tmp.df$Value)  #calculates the mean of Value column 
  objSD <-sd(tmp.df$Value)  #calculates the standard deviation of Value column
  objVar <-var(tmp.df$Value)  #Calculates the variance of Value column
  objSkewness <-skewness(tmp.df$Value, na.rm = FALSE)  #calculate the skewness of Value         Column
  objKurtosis <-kurtosis(tmp.df$Value, na.rm = FALSE)  #Calculate the kurtosis of Value     column
}

Upvotes: 1

Related Questions