Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read all nonempty csv files into list in R

Tags:

r

I would like to read a bunch of csvs into a list in R

lol<-lapply(list.files()[c(grep(Sys.Date(),list.files()))],read.csv)

Some csv files are empty though so I get

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input

(at least I think the error means)

How do I read the nonempty csv files into a list?

tryCatch does not work (or I am using it wrong)

tryCatch(toplel<-lapply(list.files()[c(grep(Sys.Date(),list.files()))],read.csv),error=function(e){print("lel")})
[1] "lel"
like image 477
discipliuned Avatar asked Dec 29 '25 17:12

discipliuned


1 Answers

Try excluding empty files by their size:

files <- list.files()[c(grep(Sys.Date(),list.files()))]
files <- files[which(file.info(files)$size>0)]

lapply(files, read.csv)
like image 126
Rentrop Avatar answered Jan 01 '26 09:01

Rentrop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!