使用readxl R将Excel文件读取到单个数据帧中 [英] reading excel files into a single dataframe with readxl R

查看:57
本文介绍了使用readxl R将Excel文件读取到单个数据帧中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆Excel文件,我想读取它们并将它们合并到一个数据框中.我有以下代码:

I have a bunch of excel files and I want to read them and merge them into a single data frame. I have the following code:

library(readxl)
files <- list.files()
f <- list()
data_names <- gsub("[.]xls", "", files)

将每个excel文件读入数据框

to read each excel file into data frames

for (i in 1:length(files)){
 assign(data_names[i], read_excel(files[i], sheet = 1, skip = 6))
 }

但是,如果我尝试将其保存在变量中,只需保存最后一个文件

but, if I try to save it in a variable, just saved the last file

for (i in 1:length(files)){
 temp <- read_excel(files[i], sheet = 1, skip = 6)
}

推荐答案

我会使用 plyr :

library(readxl)
library(plyr)
files <- list.files(".", "\\.xls")
data <- ldply(files, read_excel, sheet = 1, skip = 6)

如果要添加带有文件名的列,则可以执行以下操作:

If you wanted to add a column with the file name, you could instead do:

data <- ldply(files, function(fil) {
  data.frame(File = fil, read_excel(fil, sheet = 1, skip = 6))
}

这篇关于使用readxl R将Excel文件读取到单个数据帧中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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