applying R script prepared for single file to multiple files in the directory

前端 未结 2 1762
攒了一身酷
攒了一身酷 2020-12-05 16:54

I have prepared a r script and trying to apply the same codes in multiple files currently working with 5 sample files (but trying to learn to work with files over 100) in th

相关标签:
2条回答
  • 2020-12-05 17:27

    To read a set of csv files into a data.frame, I often use ldply from the plyr package:

    library(plyr)
    all_data = ldply(list.files(pattern = "csv"), function(fname) {
        dum = read.csv(fname)
        dum$fname = fname  # adds the filename it was read from as a column
        return(dum)
      })
    

    If you need more specific things, you extend the function you call with ldply.

    0 讨论(0)
  • 2020-12-05 17:28

    does this toy example do what you want?

    # clean up:
    rm(list = ls())
    setwd(tempdir())
    unlink(dir(tempdir()))
    
    # create some files in tempdir:
    a <- data.frame(x = 1:3, y = 4:6)
    b <- data.frame(x = 10:13, y = 14:15)
    write.csv(a, "file1.csv", row.names = F)
    write.csv(b, "file2.csv", row.names = F)
    
    # now read all files to list:
    mycsv = dir(pattern=".csv")
    
    n <- length(mycsv)
    mylist <- vector("list", n)
    
    for(i in 1:n) mylist[[i]] <- read.csv(mycsv[i])
    
    # now change something in all dfs in list:
    mylist <- lapply(mylist, function(x) {names(x) <- c("a", "b") ; return(x)})
    
    # then save back dfs:
    for(i in 1:n) 
       write.csv(file = paste("file", i, ".csv", sep = ""),
       mylist[i], row.names = F)
    
    0 讨论(0)
提交回复
热议问题