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

Asked

Viewed 667 times

1

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

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

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

Can anyone answer me how I do it using the R?

Thank you!!!

2 answers

4

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

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

If they can’t and you still want to save how 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))
})
  • Daniel, thank you for the answer! Half of these sheets are in one folder and the other half in another folder. All the files are in format. xls and I want to turn them into .csv. After turning all my spreadsheets into . csv I want to put them together so that I can work with the data by getting data on the frequency, density, dominance and abundance of the species, allowing me to understand the functioning of the communities I’m working on.

  • Daniel, thank you very much. I managed to do it the way you put it in the second option. I installed readxl and it all worked out. Now I will try to work with this data.

  • Cool! Anything we can do! Try to understand what each line of code does, this will help you to continue the analysis.

3

It is not necessary to convert an excel spreadsheet (assuming that this means .xls or .xlsx extension files) into . csv to work with it on R. Install the package xlsx and its dependencies to run the command

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

In this example, I only imported the first sheet present in the file planilha.xlsx. Just adapt this code to your needs.

  • 3

    I think I’d better use the package readxl https://github.com/hadley/readxl It does not depend on the rJava that usually causes enough trouble

  • I agree with Daniel, the readxl is much simpler and has Hadley’s signature: just do one thing, and do it well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.