r - 使用 R 和 Tidyverse,如何从多个 Excel 工作簿中导入同一种工作表
问题描述
我有多个工作簿,其中包含相同数量的工作表、工作表名称和数据。我将如何从每个工作簿中导入特定的工作表?例如,假设我只想从每个工作簿中导入“结果”选项卡的特定部分。我只想从 B11:V11 列(所有工作表的常量)导入数据,但是每个工作簿的每个列的末尾都是动态的。谢谢你的帮助。
解决方案
尝试使用以下代码:
FILES <- dir(pattern = ".xslx$")
# Make a function in order to get the data in range from "Result tab"
read_range <- function(excel_file,range_="B11:V11"){
sheets <- excel_sheets(excel_file)
# Assuming that there's always a "Result" tab
# and only one tab with this name
sheet_id <- grep("Result",sheets)
return(read_excel(excel_file,col_names= FALSE,sheet=sheet_id,range=range_))
}
# Iterate in sheets vector
data <- lapply(FILES,read_range)
# `data` is a list, yo can get the dataframe with rbind
df <- do.call("rbind",data)
希望这可以帮助
推荐阅读
- amazon-web-services - Redshift 计划查询错误通知
- godot - 空袭戈多
- javascript - WebStorm 无法自动完成样式化组件
- python - 代码从字典中删除重复字母
- google-chrome - 为什么 Google Chrome 不支持“链接”标头?
- command-line - Drush 错误:调用未定义的方法 Symfony\Component\Config\Definition\BaseNode::resetPlaceholders()
- r - 如何在“polynom::polynomial”对象中强制使用尾随零?(特别是从 ggpubr 修改 stat_lm 和 stat_regline_equation 的行为)
- linux - Linux:systemd 目标依赖项
- macos - 在 macOS 上使用 RealityKit ARView
- java - 使用 apache PDFBox 删除图像时删除空白