Export tabular data in R language to Excel file
In R language, we can use various methods to output tabular data to Excel files. Excel files are a commonly used spreadsheet format that has a wide range of applications in data analysis and visualization. This article will introduce several common methods to achieve this goal and provide corresponding source code examples.
Method 1: Using writexl
the package
writexl
is an R package for writing data to Excel files. Here is writexl
sample code for using the package to output tabular data to an Excel file:
# 安装writexl包(如果尚未安装)
install.packages("writexl")
# 加载writexl包
library(writexl)
# 创建一个示例数据框
data <- data.frame(
Name = c("Alice", "Bob", "Charlie", "David"),
Age = c(25, 32, 28, 41),
Salary = c(50000, 60000, 55000, 70000)
)
# 指定输出文件路径和文件名
output_file <- "output.xlsx"
# 使用write_xlsx函数将数据框写入Excel文件
write_xlsx(data, path = output_file)
In the above code, we first install writexl
the package and then load the package. Next, we created a sample data frame data
that contains three columns of data: name, age, and salary. We then specified the path and file name of the output file and used write_xlsx
a function to write the data frame to an Excel file.
Method Two