如何读取R编程中的多个excel表? [英] How to read multiple excel sheets in R programming?

查看:574
本文介绍了如何读取R编程中的多个excel表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个excel文件包含400张。如何使用read.xls函数将此excel文件加载到R中?请提供示例代码。

解决方案

我只是假设你想要它作为一个data.frame()和所有表格都包含相同的数据。

 库(xlsReadWrite)
sheet< - c(Sheet 1 ,Sheet 2,Sheet 3)

df< - data.frame()

(x in 1:400)
df< ; - rbind(df,read.xls(filename.xls,sheet = sheets [x]))
}

如果每张表都是自己唯一的data.frame(),您可能希望将它们放在列表中。否则,如果您希望它们作为环境中的对象,可以使用assign()。

  sheet_list<  -  list()
for(x in 1:400){
sheet_list [[x]]< - read.xls(filename.xls,sheet = sheets [x])
}

或者,没有for循环:

  sheet_list<  -  lapply(sheets,function(x)read.xls(filename.xls,sheets = x))


I have an excel file which contains 400 sheets. How can I load this excel file to R using read.xls function? Please provide sample code for this.

解决方案

I'm just assuming you want it as all one data.frame() and that all the sheets contain the same data.

library(xlsReadWrite) 
sheets <- c("Sheet 1","Sheet 2", "Sheet 3")

df <- data.frame()

for (x in 1:400) 
df <- rbind(df, read.xls("filename.xls", sheet=sheets[x]))
}

If each sheet is it's own unique data.frame() you'll probably want to put them in a list. Otherwise you can use assign() if you want them as objects in the environment.

sheet_list <- list()
for(x in 1:400) {
sheet_list[[x]] <- read.xls("filename.xls", sheet=sheets[x])
} 

Or, without a for loop:

sheet_list <- lapply(sheets, function(x) read.xls("filename.xls",sheets=x)) 

这篇关于如何读取R编程中的多个excel表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆