How to read and name different CSV files in R

前端 未结 2 1384
青春惊慌失措
青春惊慌失措 2021-01-27 03:02

I would like to work on several csv files to make some comparisons, so I wrote this code to read the different csv files I have:

path <- \"C:\\\\data\\\\\"
fi         


        
2条回答
  •  爱一瞬间的悲伤
    2021-01-27 03:45

    I would suggest using the data.table package - it's faster and for non-blank columns in the end, it converts those to NA (in my experience). Here's some code I wrote for a simialr task:

    read_func <- function(z) {
      dat <- fread(z, stringsAsFactors = FALSE)
      names(dat) <- c("start_time", "end_time", "Total", "Diffuse", "Direct", "Reflect")
      dat$start_tme <- as.POSIXct(strptime(dat$start_tme,
                        format = "%d/%m/%y %H:%M:%S"), tz = "Pacific/Easter")
      patrn <- "([0-9][0-9][0-9])\\.csv"
      dat$type <- paste("Dataset",gsub(".csv", "", regmatches(z,regexpr(patrn, z))),sep="")
      return(as.data.table(dat))
    }
    
    path <- ".//Data/" 
    file_list <- dir(path, pattern = "csv")
    file_names <- unname(sapply(file_list, function(x) paste(path, x, sep = "")))
    data_list <- lapply(file_names, read_func)
    
    dat <- rbindlist(data_list, use.names = TRUE) 
    
    rm(path, file_list, file_names)
    

    This will give you a list with each item as the data.table from the corresponding file name. I assumed that all file names have a three digit number before the extension which I used to assign a variable type to each data.table. You can change patrn to match your specific use case. This way, when you combine all of them into a single data.table dat, you can always sort/filter based on type. For example, if youwanted to plot diffuse vs direct for Dataset158 and datase222, you could do the following:

    ggplot(data = dat[type == 'Dataset158' | type == 'Dataset222'], 
           aes(x = Diffuse, y = Direct)) + geom_point()
    

    Hope this helps!

提交回复
热议问题