0

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!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Gilrob
  • 93
  • 7

1 Answers1

1

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.

Wimpel
  • 26,031
  • 1
  • 20
  • 37
  • This worked perfectly thanks! Out of interest, how would you combine it into one line please? – Gilrob Aug 11 '23 at 01:56
  • you can nest the functions, like `rbindlist(lapply(list.files( blahblah ), fread), use.names = TRUE)` – Wimpel Aug 18 '23 at 15:25