Export data from R to Excel

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Export data from R to Excel

Message par Clippy »

I am writing code to export database from R into Excel, I have been trying others code including:

```
write.table(ALBERTA1, "D:/ALBERTA1.txt", sep="\t")
write.csv(ALBERTA1,":\ALBERTA1.csv")
your_filename_in_R = read.csv("ALBERTA1.csv")
your_filename_in_R = read.csv("ALBERTA1.csv")
write.csv(df, file = "ALBERTA1.csv")
your_filename_in_R = read.csv("ALBERTA1.csv")
write.csv(ALBERTA1, "ALBERTA1.csv")
write.table(ALBERTA1, 'clipboard', sep='\t')
write.table(ALBERTA1,"ALBERTA1.txt")
write.table(as.matrix(ALBERTA2),"ALBERTA2.txt")
write.table(as.matrix(vecm.pred$fcst$Alberta_Females[,1]), "vecm.pred$fcst$Alberta_Females[,1].txt")
write.table(as.matrix(foo),"foo.txt")
write.xlsx(ALBERTA2, "/ALBERTA2.xlsx")
write.table(ALBERTA1, "D:/ALBERTA1.txt", sep="\t").

```

Other users of this forum advised me this:

```
write.csv2(ALBERTA1, "ALBERTA1.csv")
write.table(kt, "D:/kt.txt", sep="\t", row.names=FALSE)

```

You can see on the pictures the outcome I have got from the code above. But this numbers can't be used to make any further operations such as addition with other matrices.

Has someone experienced this kind of problems?
Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Re: Export data from R to Excel

Message par Clippy »

Another option is the `openxlsx`-package ([CRAN](https://cran.r-project.org/web/packages/openxlsx/index.html), [website](https://ycphs.github.io/openxlsx/index.html), [GitHub](https://github.com/ycphs/openxlsx)). It doesn't depend on [java](/questions/tagged/java) and can read, edit and write *Excel*-files. From the description from the package:

>

openxlsx simplifies the the process of writing and styling Excel xlsx files from R and removes the dependency on Java

Example usage:

```
library(openxlsx)

# read data from an Excel file or Workbook object into a data.frame
df <- read.xlsx('name-of-your-excel-file.xlsx')

# for writing a data.frame or list of data.frames to an xlsx file
write.xlsx(df, 'name-of-your-excel-file.xlsx')

```

Besides these two basic functions, the `openxlsx`-package has a host of other functions for manipulating *Excel*-files.

For example, with the `writeDataTable`-function you can create formatted tables in an *Excel*-file.
Répondre

Revenir à « Excel & VBA »