如何使用 R 将大型 Excel 文件拆分为多个 Excel 文件
How to split large Excel file into multiple Excel files using R
我正在寻找一种使用 R 将大型 Excel 文件拆分为多个较小的 Excel 文件的方法。
具体来说,我想做三件事:
- 我有一个大型数据集,其中包含有关学生的信息(他们的学校、学校所在的地区、考试成绩 A、考试成绩 B),我想将其拆分成单独的文件,一个文件每所学校包含就读该特定学校的所有学生。
- 我还希望所有 Excel 文件都包含一个图像,覆盖每个 Excel 文件的第一行和 A、B、C 和 D 列。数据集中所有学校的图像都相同。
- 最后,我还希望 Excel 文件在创建后最终位于我桌面上的各个文件夹中。文件夹名称将是学校所在的区域。一个区域大约有 3-5 所学校,因此该文件夹将包含 3-5 Excel 个文件,每个学校 1 个。
我的数据结构如下:
Area
School
Student ID
Test score A
Test score B
North
A
134
24
31
North
A
221
26
33
South
B
122
22
21
South
B
126
25
25
我的数据涵盖了位于 5 个不同地区的大约 200 所学校。
任何有关如何执行此操作的指导将不胜感激!
正如一些评论所引用的,如果不知道您的具体操作环境和文件夹结构,这将很难解决,我使用 Windows 10/ C 盘用户文件夹解决了这个问题,但您可以自定义系统。您将需要一个文件夹,其中包含学校的所有图像,并以学校名称(或我创建的 ID)保存,并且它们都需要采用相同的格式(JPG 或 PNG)。另外,您需要为要输出到的每个区域创建文件夹(openxlsx 可以写入文件,但不能为您创建文件夹)。完成这些设置后,类似于此的内容应该适合您,但我强烈建议您参考 openxlsx 文档以获取更多信息:
library(dplyr)
library(openxlsx)
# Load your excel file into a df
# g0 = openxlsx::read.xlsx(<your excel file & sheet..see openxlsx documentation>)
# Replace this tibble with your actual excel file, this was just for an example
g0 = tibble(
Area = c("North","North","North","North"),
School = c("A","A","B","B"),
Student_ID = c(134,221,122,126),
test_score_a = c(24,26,22,25),
test_score_b = c(31,33,21,25))
# Need a numeric school id for the loop
g0$school_id = as.numeric(as.factor(g0$School))
# Loop through schools, filter using dplyr and create a sheet per school
for (i in 1:n_distinct(g0$school_id)){
g1 = g0 %>%
filter(school_id == i)
## Create a new workbook
wb <- createWorkbook(as.character(g1$School))
## Add some worksheets
addWorksheet(wb, as.character(g1$School))
## Insert images
## I left the image as a direct path for example but you can change to a
## relative path once you get it working
img <- system.file("C:","Users","your name","Documents","A","A.jpg", package = "openxlsx")
insertImage(wb, as.character(g1$School), img, startRow = 5, startCol = 3, width = 6, height = 5)
## Save workbook
saveWorkbook(wb, paste0("C://Users//your name//Documents//",g0$Area,"//",g0$school,".xlsx"), overwrite = TRUE)
}
我正在寻找一种使用 R 将大型 Excel 文件拆分为多个较小的 Excel 文件的方法。
具体来说,我想做三件事:
- 我有一个大型数据集,其中包含有关学生的信息(他们的学校、学校所在的地区、考试成绩 A、考试成绩 B),我想将其拆分成单独的文件,一个文件每所学校包含就读该特定学校的所有学生。
- 我还希望所有 Excel 文件都包含一个图像,覆盖每个 Excel 文件的第一行和 A、B、C 和 D 列。数据集中所有学校的图像都相同。
- 最后,我还希望 Excel 文件在创建后最终位于我桌面上的各个文件夹中。文件夹名称将是学校所在的区域。一个区域大约有 3-5 所学校,因此该文件夹将包含 3-5 Excel 个文件,每个学校 1 个。
我的数据结构如下:
Area | School | Student ID | Test score A | Test score B |
---|---|---|---|---|
North | A | 134 | 24 | 31 |
North | A | 221 | 26 | 33 |
South | B | 122 | 22 | 21 |
South | B | 126 | 25 | 25 |
我的数据涵盖了位于 5 个不同地区的大约 200 所学校。
任何有关如何执行此操作的指导将不胜感激!
正如一些评论所引用的,如果不知道您的具体操作环境和文件夹结构,这将很难解决,我使用 Windows 10/ C 盘用户文件夹解决了这个问题,但您可以自定义系统。您将需要一个文件夹,其中包含学校的所有图像,并以学校名称(或我创建的 ID)保存,并且它们都需要采用相同的格式(JPG 或 PNG)。另外,您需要为要输出到的每个区域创建文件夹(openxlsx 可以写入文件,但不能为您创建文件夹)。完成这些设置后,类似于此的内容应该适合您,但我强烈建议您参考 openxlsx 文档以获取更多信息:
library(dplyr)
library(openxlsx)
# Load your excel file into a df
# g0 = openxlsx::read.xlsx(<your excel file & sheet..see openxlsx documentation>)
# Replace this tibble with your actual excel file, this was just for an example
g0 = tibble(
Area = c("North","North","North","North"),
School = c("A","A","B","B"),
Student_ID = c(134,221,122,126),
test_score_a = c(24,26,22,25),
test_score_b = c(31,33,21,25))
# Need a numeric school id for the loop
g0$school_id = as.numeric(as.factor(g0$School))
# Loop through schools, filter using dplyr and create a sheet per school
for (i in 1:n_distinct(g0$school_id)){
g1 = g0 %>%
filter(school_id == i)
## Create a new workbook
wb <- createWorkbook(as.character(g1$School))
## Add some worksheets
addWorksheet(wb, as.character(g1$School))
## Insert images
## I left the image as a direct path for example but you can change to a
## relative path once you get it working
img <- system.file("C:","Users","your name","Documents","A","A.jpg", package = "openxlsx")
insertImage(wb, as.character(g1$School), img, startRow = 5, startCol = 3, width = 6, height = 5)
## Save workbook
saveWorkbook(wb, paste0("C://Users//your name//Documents//",g0$Area,"//",g0$school,".xlsx"), overwrite = TRUE)
}