How to save a series of xls spreadsheets in csv using R? [closed]

1

I have 198 worksheets all in Excel (xls format) and need to pass them to .csv format so that I can work with them in R. These worksheets are saved inside a folder on my computer all in .xls format

After converting them to .csv I need to unite all of them in order to obtain a similarity table, containing in the lines my study areas and in columns my plant species, filling with data of abundance of species for each area sampled. In addition to similarity, I need to calculate parameters that are structuring my plant communities, such as frequency, density, and dominance.

So, at the beginning, I would like to have a command in R where it can automatically transform my .xls tables into .csv so that I can start my journey, since it will take me a long time to save one by one ...

Can anyone answer me how do I do this using R?

Thank you !!!

    
asked by anonymous 02.09.2016 / 19:27

2 answers

4

If all of these worksheets are in a single folder, you can use something like this. I'm guessing that all spreadsheets have the same format.

plans <- list.files("caminho/da/pasta", full.names = T)
bases <- plyr::ldply(plans, readxl::read_excel)

If they do not, and you still want to save as csv to work easier on R, you can do something like this:

plans <- list.files("caminho/da/pasta", full.names = T)
plyr::l_ply(plans, function(p){
    b <- readxl::read_excel(p)
    write.csv2(b, file = gsub("xls", "csv", p))
})
    
02.09.2016 / 19:49
2

You do not need to convert an excel spreadsheet (assuming this means files with .xls or .xlsx extension) in .csv to work with in R. Install the xlsx package and its dependencies to run the

library(xlsx)
dados <- read.xlsx(file="planilha.xlsx", sheetIndex=1)

In this example, I imported only the first worksheet in the planilha.xlsx file. It is enough that you adapt this code to your needs.

    
02.09.2016 / 19:39