r - 循环并阅读多张相同的工作表并保存为单独的数据框/变量
问题描述
install.packages("readxl")
library("readxl")
data <- read_excel("spreadsheet.xlsx")
sheets <- excel_sheets("spreadsheet.xlsx")
sheetList <- as.list(sheets) #convert sheets to list
for (s in sheetList){
read_excel("spreadsheet.xlsx",sheet=s)
} #want to do such that when looping through each sheet, it would read in the data and be assigned its own 'variable'(dataframe)
解决方案
我不会使用 for 循环。使用 lapply 更容易遍历列表。这是我将如何做到的。
library(readxl)
sheets_to_read <- excel_sheets("spreadsheet.xlsx") ##Obtain the sheets in the workbook
list_with_sheets <- lapply(sheets_to_read,
function(i)read_excel("spreadsheet.xlsx", sheet = i))
names(list_with_sheets) <- sheets_to_read ##Add names
list2env(mylist ,.GlobalEnv) ##This function should put them all in the global environment
推荐阅读
- cakebuild - 将多个参数/参数传递给目标
- c# - 比较它们的 int id 属性时如何强制执行类类型
- python - flask run 不起作用,但 python server.py 可以!环境变量集
- oracle - 在 JSON LIST PLSQL 中解析 JSON LIST
- linux - 为 mp3/wav 编译自定义 FFMPEG 到 flac 转换
- powershell - 将 Powershell 命令转换为 CMD
- amazon-web-services - ECS / Fargate 不时在凌晨 12 点无故取消注册目标组中的容器
- kubernetes - 从 docker 私有存储库中提取 K8s 问题
- mysql - 在select knex js中计数
- powershell - 具有多态性和单例模式的 PowerShell 类