如何在 R 中读取大型数据集

机器算法验证 r 大数据
2022-03-16 06:58:30

尝试在 R 中读取大型数据集时,控制台显示以下错误:

data<-read.csv("UserDailyStats.csv", sep=",", header=T, na.strings="-", stringsAsFactors=FALSE)
> data = data[complete.cases(data),]
> dataset<-data.frame(user_id=as.character(data[,1]),event_date= as.character(data[,2]),day_of_week=as.factor(data[,3]),distinct_events_a_count=as.numeric(as.character(data[,4])),total_events_a_count=as.numeric(as.character(data[,5])),events_a_duration=as.numeric(as.character(data[,6])),distinct_events_b_count=as.numeric(as.character(data[,7])),total_events_b=as.numeric(as.character(data[,8])),events_b_duration= as.numeric(as.character(data[,9])))
Error: cannot allocate vector of size 94.3 Mb
In addition: Warning messages:
1: In data.frame(user_msisdn = as.character(data[, 1]), calls_date = as.character(data[,  :
  NAs introduced by coercion
2: In data.frame(user_msisdn = as.character(data[, 1]), calls_date = as.character(data[,  :
  NAs introduced by coercion
3: In class(value) <- "data.frame" :
  Reached total allocation of 3583Mb: see help(memory.size)
4: In class(value) <- "data.frame" :
  Reached total allocation of 3583Mb: see help(memory.size)

有谁知道如何读取大型数据集?UserDailyStats.csv 的大小约为 2GB。

4个回答

两个基本的东西:

  1. 该投诉解决了 R 会话中的所有内存,而不仅仅是您正在加载的一个对象。除非您使用类似的东西,否则ff会话中的所有内容都在内存中。
  2. 一个 Windows,你需要指定 R 可以使用多少内存。看看help(memory.limit). 即使您使用的是 64 位,它也不会默认使用所有可用内存。

bigmemory否则,如果仍然存在问题,您可以考虑使用它来处理更大的数据集。一些相关来源:

作为最后的建议,您可以gc()在运行命令之前尝试调用以释放内存,尽管原则上 R 会根据需要自动执行此操作。

你在哪个平台上运行 R?机器有多少物理和虚拟内存?

此外,您可能会发现以下相关内容:http ://stat.ethz.ch/R-manual/R-devel/library/base/html/Memory-limits.html

我完全同意德克的回答。一个建议。我发现在评估大型数据库时使用 AWK 或其他编程语言非常有用。因此,我能够过滤要包含在分析中的数据,从而减少数据集的最终大小。

此外,在您的代码中,您将相同的数据集复制两次(数据和数据集)。如果要将变量定义为因子、数字等,可以在 read.table 函数中使用 colClasses 选项。

由于您使用的是 64 位 Windows,因此请确保您已安装并正在运行 64 位版本的 R for Windows。然后,按照 Gary King 页面上的说明进行操作: