How to read and name different CSV files in R

前端 未结 2 1381
青春惊慌失措
青春惊慌失措 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!

    0 讨论(0)
  • 2021-01-27 03:48

    You're having a problem because your csv files have a blank column at the end... which makes your data end in a comma:

    04/09/14 00:01:00,04/09/14 00:01:00,2.221220E-003,5.797364E-004,0.000000E+000,1.641484E-003,

    This leads R to think your data consists of 7 columns rather than 6. The correct solution is to resave all your csv files correctly. Otherwise, R will see 7 columns but only 6 columnnames, and will logically think that the first column is rownames. Here you can apply the patch we came up with @konradrudolph:

    library(tibble) df %>% rownames_to_column() %>% setNames(c(colnames(.)[-1], 'DROP')) %>% select(-DROP)

    where df is the data from the csv. But patches like this can lead to unexpected results... better save the csv files correctly.

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