r - 在 R 中使用多张纸读取 xlsx 以删除重复项
问题描述
我有一个嵌入了多张工作表的 excel 文件。我的主要目标是基本上删除在一张纸中多次出现的所有行,并且必须为每张纸执行此操作。我已经编写了下面的代码,但代码只是读取第一张纸,并在第一行和第一列给出“...”。有人可以帮我解决我可能出错的地方。谢谢先进
**config_file_name <- '/RBIAPI3tables.xlsx'
config_xl <- paste(currentPath,config_file_name,sep="")
config_xl_sheets_name <- excel_sheets(path = config_xl) # An array of sheets is created. To access the array use config_xl_sheets[1]
count_of_xl_sheets <- length(config_xl_sheets_name)
# Read all sheets in the file as separate lists
list_all_sheets <- lapply(config_xl_sheets_name, function(x) read_excel(path = config_xl, sheet = x))
names (list_all_sheets) <- config_xl_sheets_name # Change the name of all the lists to excel file sheets name
count_of_list_all_sheets <- length(list_all_sheets) # to get the data frame of each list use list_all_sheets[[Config]]
# Create data frame for each sheet Assign the sheet name to the data frame
for (i in 1:count_of_list_all_sheets)
{
assign(x= trimws(config_xl_sheets_name[i]), value = data.frame(list_all_sheets[[i]]))
updateddata = unique(list_all_sheets[[i]])
}
write.xlsx(updateddata,"Unique3tables.xlsx",showNA = FALSE)**
解决方案
这是我的方法
library(readxl)
library(data.table)
library(openxlsx)
file.to.read <- "./testdata.xlsx"
sheets.to.read <- readxl::excel_sheets(file.to.read)
# read sheets from the file to a list and remove duplicate rows
L <- lapply(sheets.to.read, function(x) {
data <- setDT(readxl::read_excel(file.to.read, sheet = x))
#remove puplicates
data[!duplicated(data), ]
})
# create a new workbook
wb <- createWorkbook()
# create new worksheets an write to them
for (i in seq.int(L)) {
addWorksheet(wb, sheets.to.read[i])
writeData(wb, i, L[[i]] )
}
# write the workbook to disk
saveWorkbook(wb, "testdata_new.xlsx")
推荐阅读
- typescript - 关于 X 类型参数的 getParam 错误不可分配给“导航”类型的参数 | “屏幕道具” | “导航选项”'
- visual-studio - 使 Visual Studio 2019 始终从开始栏以管理员身份运行最近的解决方案列表
- ruby-on-rails - 重构列表排序方法的控制器代码
- google-chrome-extension - Google Chrome 的开发工具如何模拟设备模式?如何使用扩展复制它?
- java - 在套接字连接中具有交换密钥的 Java RSA
- python - 让 Python 一遍又一遍地问问题,直到某些输入
- excel - 如何使用 vba 代码刷新外部工作簿中的数据?
- bash - 无法使用期望脚本删除文件
- vapor - 如何输出发送到端点的任何 JSON?
- wcf - C#- 使用 IClientChannel 接口而不实现所有方法,和/或不以凌乱的方式进行转换