首页 > 解决方案 > 如何在不遇到内存问题的情况下对几百万行进行 vlookup?

问题描述

希望编写可以执行 vlookup 的代码。

我有几个充当查找表的 excel 文件(每个包含 ~1m 行(excel 文件限制)。

然后,我有一个包含两列需要查找的 excel 表——这两列不能合并,因为结果必须分开。

首先,我加载包含要查找的表的文件:

All <- lapply(filenames_list,function(filename){
  print(paste("Merging",filename,sep = " "))
  read.xlsx(filename)
})
df <- do.call(rbind.data.frame, All)

然后我加载我要查找的文件:

LookUpID1 <- read.xlsx(paste(current_working_dir,"/LookUpIDs.xlsx", sep=""), sheet = 1, startRow = 1, colNames = TRUE, cols = 1, skipEmptyRows = TRUE, skipEmptyCols = TRUE)
LookUpID2 <- read.xlsx(paste(current_working_dir,"/LookUpIDs.xlsx", sep=""), sheet = 1, startRow = 1, colNames = TRUE, cols = 2, skipEmptyRows = TRUE, skipEmptyCols = TRUE)

我需要加载文件两次,因为我需要在第 1 列和第 2 列上执行查找。

然后是实际的vlookup:

# Matching ID
FoundIDs1 <- merge(df, LookUpID1)
FoundIDs2 <- merge(df, LookUpID2)
FoundIDs <- merge(FoundIDs1, FoundIDs2, by = NULL)

问题是我的电脑在运行代码的最后一部分(实际的 Vlookup)时内存不足;

错误:无法分配大小为 1715.0 Gb 的向量

标签: rout-of-memory

解决方案


推荐阅读