将数据从 R 导出到 Excel

问题描述:

我正在编写代码将数据库从 R 导出到 Excel,我一直在尝试其他代码,包括:

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

write.table(ALBERTA1, "D:/ALBERTA1.txt", sep="	")
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='	')
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="	").

此论坛的其他用户建议我这样做:

Other users of this forum advised me this:

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

你可以在图片上看到我从上面的代码中得到的结果.但是这个数字不能用于进行任何进一步的操作,例如与其他矩阵的加法.

You can see on the pictures the outcome I have got from this codes 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?

另一个选项是 openxlsx 包.它不依赖于 并且可以阅读,编辑和编写 Excel 文件.来自包装的描述:

Another option is the openxlsx-package. It doesn't depend on java and can read, edit and write Excel-files. From the description from the package:

openxlsx 简化了从 R 编写和设置 Excel xlsx 文件的过程,并消除了对 Java 的依赖

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

示例用法:

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')

除了这两个基本功能之外,openxlsx-包还有许多其他用于操作 Excel-文件的功能.

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

例如,使用 writeDataTable 函数,您可以在 Excel 文件中创建格式化表格.

For example, with the writeDataTable-function you can create formatted tables in an Excel-file.