R语言:怎么把同一excel中不同Sheet的数据提取到一起?

怎么把同一excel中不同Sheet的数据提取到一起?

问题描述:这一个excel中共有32个Sheet,我要将每个sheet中的几列提取出来到一个新的excel中。

# 加载所需的库
library(readxl)
library(data.table)
library(writexl)

rm(list = ls()) #Clean all
# 指定Excel文件的路径
file_path <- "Data File.xlsx"

# 获取所有工作表的名称
sheet_names <- excel_sheets(file_path)

# 创建一个空的数据框,用于存储结果
combined_df <- data.frame(matrix(numeric(0), nrow = 0, ncol = 5))

# 循环读取每个工作表
for(sheet in sheet_names) {
  #sheet = '#1.China Agric.University' #这是第一个Sheet的名称用于测试
  temp_df <- read_excel(file_path, sheet = sheet)
  temp_df = temp_df[12:16] #提取每一个sheet的12-16列
  
  # 将当前工作表的数据添加到组合数据框中
  combined_df <- rbind(combined_df, temp_df)
  cat("Sheet:", sheet, "- Rows:", nrow(temp_df), "\n")
}

write_xlsx(combined_df,"E:\\1_Research\\8_Erhai\\3_Data\\3_我整理\\Site2data.xlsx")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值