Agaz Wani
Agaz Wani

Reputation: 5684

Converting .xls to .txt and viceversa

I want to know if conversion of file format is possible in R. If i have a .xls file and would like to convert to a .txt format or viceversa. likewise is conversion of other formats possible .xls to .csv and so on.

Upvotes: 1

Views: 4246

Answers (1)

Pierre L
Pierre L

Reputation: 28441

Read in the .xls file with whatever package you like. I use openxlsx, but use xlsx for that specific file type.

library(xlsx)
data <- read.xlsx2(file, sheet, ...)

write.table(data, "filename.txt", ...) 

#or

write.csv(data, "filename.csv", ...)

Or if you want a function, try something like:

library(xlsx)
xls.csv.converter <- function(File, Sheet=1) {
d <- read.xlsx2(File, Sheet)
write.csv(d, paste0(getwd(), "/", substr(File, 1, nchar(File)-4),".csv"))
}

xls.csv.converter("mydata.xls")

Note that the function search for the file in your working directory and the file will be written to your working directory.

Upvotes: 3

Related Questions