Which is the best method to apply a script repetitively to n .csv files in R?

后端 未结 2 379
感情败类
感情败类 2020-11-28 11:39

My situation:

  1. I have a number of csv files all with the same suffix pre .csv, but the first two characters of the file name are different (ie
相关标签:
2条回答
  • 2020-11-28 11:54

    I find a for loop and lists is well enough for stuff like this. Once you have a working set of code it's easy enough to move from a loop into a function which can be sapplyied or similar, but that kind of vectorization is idiosyncratic anyway and probably not useful outside of private one-liners.

    You probably want to avoid assigning to multiple objects with different names in the workspace (this a FAQ which usually comes up as "how do I assign() . . .").

    Please beware my untested code.

    A vector of file names, and a list with a named element for each file.

    files <- c("AA01.csv", "AA02.csv")
    lst <- vector("list", length(files))
    names(lst) <- files
    

    Loop over each file.

    library(timeSeries)
    
    for (i in 1:length(files)) {
        ## read strings as character
        tmp <- read.csv(files[i], stringsAsFactors = FALSE)
        ## convert to 'timeDate'
        tmp$tfrm <- timeDate(paste(tmp$cdt, tmp$ctm),format ="%Y/%m/%d %H:%M:%S"))
        ## create timeSeries object
        obj <- timeSeries(as.matrix(tmp$Value), tmp$tfrm)
        ## store object in the list, by name
        lst[[files[i]]] <- as.xts(obj)
    }
    
    ## clean up
    rm(tmp, files, obj)
    

    Now all the read objects are in lst, but you'll want to test that the file is available, that it was read correctly, and you may want to modify the names to be more sensible than just the file name.

    Print out the first object by name index from the list:

    lst[[files[1]]]
    
    0 讨论(0)
  • 2020-11-28 12:14

    Be sure to use Rs dir command to produce the list of filenames instead of manually entering them in.

    filenames = dir(pattern="*01.csv")
    for( i in 1:length(filenames) )
    {
      ...
    
    0 讨论(0)
提交回复
热议问题