R语言 导入多张Excel表
在这篇文章中,我们将看到如何将多个Excel工作表导入R语言中。
Excel为我们提供了多个工作表。例如,在下面的Excel工作簿StudentData中,我们有两个工作表–工作表1是学生详情,工作表2是科目详情。
为了将多个Excel工作表导入R语言,我们必须首先在R语言中安装一个名为 readxl 的包 _ 。_ 成功安装该包后,我们必须使用R语言中的 库函数 加载该包。
install.packages('readxl')
一旦我们在RStudio中完全安装并加载了软件包,接下来的工作就是导入excel工作簿并检查其包含的工作表数量。我们可以使用 excel_sheets 函数来做这件事。
library("read_excel")
# Importing the excel workbook for
# checking the number of sheets it contains
excel_sheets("StudentData.xlsx")
输出
'StudentDetails' 'SubjectDetails'
我们有一个名为 “StudentData “的Excel文件,我们已经把它保存在我们的工作目录中。它包含两个名为 StudentDetails 和 SubjectDetails 的工作表 。 在R中,我们有一个名为 _ read_excel()_ 的函数,我们将用它来将特定的工作表导入R中。
语法: read_excel(arg)
代码
# Importing specific sheets into R using the read_excel()
StudentDet<-read_excel("StudentData.xlsx",
sheet = 1)
StudentDet<-read_excel("StudentData.xlsx",
sheet = "StudentDetails")
SubjectDet<-read_excel("StudentData.xlsx",
sheet = "SubjectDetails")
# For viewing the details of sheet 1
head(StudentDet)
# For viewing the details of sheet 2
head(SubjectDet)
输出