How can I turn the filename into a variable when reading multiple csvs into R

后端 未结 4 564
梦毁少年i
梦毁少年i 2021-01-03 08:58

I have a bunch of csv files that follow the naming scheme: est2009US.csv.

I am reading them into R as follows:

myFiles <- list.files(path=\"~/Dow         


        
相关标签:
4条回答
  • 2021-01-03 09:20
    Nrows <- lapply( lapply(myFiles, read.csv, header=TRUE), NROW)
    # might have been easier to store: lapply(myFiles, read.csv, header=TRUE)
    myDB$grp <- rep( myFiles, Nrows) )
    
    0 讨论(0)
  • 2021-01-03 09:25

    You can create the object from lapply first.

    Lapply <- lapply(myFiles, read.csv, header=TRUE))
    names(Lapply) <- myFiles
    for(i in myFiles) 
        Lapply[[i]]$Source = i
    do.call(rbind, Lapply)
    
    0 讨论(0)
  • 2021-01-03 09:33

    plyr makes this very easy:

    library(plyr)
    paths <- dir(pattern = "\\.csv$")
    names(paths) <- basename(paths)
    
    all <- ldply(paths, read.csv)
    

    Because paths is named, all will automatically get a column containing those names.

    0 讨论(0)
  • 2021-01-03 09:37

    You can avoid looping twice by using an anonymous function that assigns the file name as a column to each data.frame in the same lapply that you use to read the csvs.

    myDB <- do.call("rbind", lapply(myFiles, function(x) {
      dat <- read.csv(x, header=TRUE)
      dat$fileName <- tools::file_path_sans_ext(basename(x))
      dat
    }))
    

    I stripped out the directory and file extension. basename() returns the file name, not including the directory, and tools::file_path_sans_ext() removes the file extension.

    0 讨论(0)
提交回复
热议问题