rfread

fread() reading everything in as 'character'


Apologies straight away for no MRE.

I am using fread() to pile about 45 different .csv files one on top of each other in a single dataframe.

dat<-fread(cmd='cat data/*.csv, header=T)

I copied this from webb's answer to this question.

It's working well and reading in nicely, but every column is being saved as character at the moment when about 90% of them are actually numeric.

dat<-fread(cmd='cat data/*.csv, header=T,colClasses="numeric")

I tried to coerce everything to numeric but that didn't work. Is there any way to fix this Thanks!


Solution

  • Try this (data.table) approach

    library(data.table)
    files.to.read <- list.files(path = "./data", 
                                pattern = ".*\\.csv$", 
                                full.names = TRUE, 
                                recursive = FALSE)
    L <- lapply(files.to.read, fread)
    DT <- rbindlist(L, use.names = TRUE, fill = TRUE)
    

    I used 3 lines of code for readability. But you van easily convert them to a single line if needed.