jeudi 30 novembre 2017

Add a condition to start a function in R

I have created a script that open all csv and xlsx files a directory contains. My issue appears when my directory doesn't contain one type of file. (ex : 5 csv and 0 xlsx OR 0 csv and 14 xlsx) Here is what my script looks like:

#step 1
file.list <- list.files(pattern='*.csv')
csv_df <- lapply(file.list, read.csv, header=TRUE)

#step 2
file.list <- list.files(pattern='*.xlsx')
xlsx_df <- lapply(file.list, read_excel_function)

I got an error message:

Error in do.call("rbind", xlsx_df) :
le second argument doit être une liste (the second argument must be a list)

Do you have any idea on how to add a condition that skip one step if the list.files return nothing?

Aucun commentaire:

Enregistrer un commentaire