如何将循环应用于我的R脚本

时间:2014-04-12 00:14:48

标签: r loops

我将在前面提出我的问题,即我是R的新手并且从未使用任何其他语言进行编程。我一直在阅读本网站上关于循环和应用函数的主题。这个问题可能被视为重复,但我找不到满足我需求的解决方案。我有一个简单的脚本,我想应用于文件夹中的所有文件。我希望发生的是脚本将应用于文件夹中的每个文件,并提供一个输出文件,其中包含每个文件的结果。我原本以为这将是一个批处理程序,但从我的所有搜索看起来它看起来更合适。

我尝试从applying R script prepared for single file to multiple files in the directory实施该方法。

这就是我目前的情况:

    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
     }

此代码的问题在于它似乎只在列表中的第一个文件上运行,因此不会处理文件夹中的其余文件。

1 个答案:

答案 0 :(得分:1)

我一直在处理这类问题。你必须在dir()命令中做一些小改动。这是新脚本

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
}