如何在R中将数据框导出到Excel

时间:2020-02-23 14:43:48  来源:igfitidea点击:

如果我告诉您,您可以在几分钟内将数据帧导出到R中的excel,该怎么办?借助于writexl和openxlsx之类的功能,这是可能的。
看起来很有趣?在本教程中,我们将看到如何将数据帧导出到R中的excel。

在深入学习这些教程之前,让我们了解什么是writexl和openxlsx函数。

从安装所需的软件包开始

writexl:writexl是R中的一个库,能够将R数据帧写入excel文件。
要安装此软件包,请运行以下代码。

install.packages('writexl')

Openxlsx:R库,它在处理数据并将其导出到Excel中非常有用。
要安装此软件包,请运行以下代码。

install.packages('openxlsx')

创建数据框并导出到Excel

好吧,希望您已经成功安装了这些软件包。
现在,让我们在R中创建一个数据框,然后使用" writexl"功能将其导出到Excel。

#creates a dataframe in R
mydataframe<- data.frame(name=c('John','Angelina','Lisa','Joseph','Antonio'), 
age=c(32,30,26,23,28),
gender=c('male','female','female','male','male'))

创建此数据框后,让我们使用writexl函数将其导出到Excel。

#import the required library
library(writexl)
#exports the data to excel and dumps it to the working directory/folder
#to see your working directory run 'getwd()' in R studio

mydataframe<- data.frame(name=c('John','Angelina','Lisa','Joseph','Antonio'), 
age=c(32,30,26,23,28),
gender=c('male','female','female','male','male'))

write_xlsx(mydataframe, 'export_a_dataframe_to_excel.xlsx')

输出的excel文件如下所示。

在R中导入数据集并将其导出到Excel

在上一节中,您已经在R中导出了一个简单的数据框以使其表现出色。
其中我们将导入数据集或者CSV文件,并将其导出到Excel文件。

#imports the iris data set
x<-datasets::iris
View(x) 
#exports the data frames to excel 
write_xlsx(x, 'Exporitng_a_csv_file_to_excel.xlsx')

通过使用openxlsx处理数据将数据框导出到Excel

到目前为止,我们无需任何数据操作即可将数据框导出到Excel。
但是,如果您需要导出数据并进行一些有意义的更改怎么办?函数" dplyr"和" openxlsx"的使用。

Dplyr库将帮助您进行数据操作,即您可以通过添加列,行或者百分比等来更改数据集。

Openxlsx函数允许您进行所有可能的更改,例如添加样式,字体,颜色,还可以使用此函数创建工作表。

下图显示了R中可用的基本数据集。

让我们使用dplyr对数据框的标题进行一些更改。
其中我们正在更改标题的颜色和居中对齐。
我们还更改了"文盲"列中的百分比值。

执行以下代码以创建具有新标题样式的excel文件。

#imports the required libraries
library(dplyr)
library(rio)
library(openxlsx)

#creates a data frame with percentage and manipulated it using dplyr
mydata<- as.data.frame(state.x77)%>%
 mutate(State=row.names(state.x77),Illiteracy=Illiteracy/100)%>%
 select(State, Population:Area)

#creates a new headline style
my_headline<-createStyle(halign = "center",textDecoration = "Bold",fontSize = 14,fontColour = "blue")

#changes the headline style
write.xlsx(mydata, "new_header_style.xlsx", headerStyle=my_headline)
#chnages the values in illeteracy column as percentages 
class(mydata$Illiteracy)<-"percentage"

#exports the excel file with new header style and percentage of values
write.xlsx(mydata, 'with_percentage_stye.xlsx', headerStyle=my_headline)

具有新标题样式和对齐样式的数据框如下所示。

在R中创建新的表格样式并导出到Excel

您还可以通过将这段代码添加到上面的代码中来更改表格的主题或者样式。

#creates a new table style or theme
write.xlsx(mydata, "new_table_style.xlsx", asTable = T, tableStyle='TableStyleLight1')

好了,我们已经使用writexl和openxlsx函数创建了数据框,导入了数据框并导出到excel。
我们进行了数据处理,并以崭新的外观导出了数据框。

但是,您可以在R中进行其他操作。
让我们来研究一下。

我们可以在R中创建工作簿并导出到Excel吗?

答案将是一个很大的"是"。
您可以创建一个完整的工作簿,并向其中添加工作表以及一些数据和一些有意义且对读者有意义的更改。

让我们开始吧!!!

#creates a new workbook with new worksheet
myworkbook<-createWorkbook()
addWorksheet(myworkbook,"MySheet")
writeData(myworkbook,"MySheet",mydata)
 
#adds the different nemurical styles and the number formates to data frame 
my_dollar_style<-createStyle(numFmt = "{代码},0")
my_percentage_style<-createStyle(numFmt = "0.0%")
my_comma_style<-createStyle(numFmt = "0,0")
 
#incorporates the styles to data
addStyle(myworkbook, "MySheet", style = my_dollar_style, rows = 2:nrow(mydata),cols = 3)
addStyle(myworkbook,"MySheet",style = my_percentage_style,rows = 2:nrow(mydata),cols = 4)
addStyle(myworkbook,"MySheet",style = my_comma_style,rows = 2:nrow(mydata), cols = 9)

#save the workbook with new number formats as well
saveWorkbook(myworkbook,"with_numerical_styling.xlsx", overwrite = T)

在下面的工作簿中,您可以看到收入列中已经添加了美元符号,并且值之间用逗号分隔。