首页 > 解决方案 > 在 R 中的 expand.grid() 上出现内存错误

问题描述

我遇到了内存问题,需要帮助来解决它。由于我公司的保密问题,我无法在此处发布确切的代码或结果。但是,我使用了如下的虚拟引用

有2个数据框如下

data frame A looks like
id  x_1  x_2   x_3  x_4
1  data   data  data data
2  data   data  data data
3  data   data  data data

data frame B looks like
id_1  x_1  x_2   x_3  x_4
1    data   data  data data
2    data   data  data data
3    data   data  data data

希望得到 A 和 B 的第一列的组合结果为

id  id_1
1    1
1    2
1    3
2    1
2    2
2    3
3    1
3    2
3    3

因此,使用 expand.grid 作为:

myLoadedData1 <- expand.grid(A$id,B$id)

当 A 和 B 数据框各有 8000 条记录时,expand.grid 工作正常。

由于无法避免的可扩展性,两个数据帧中的记录现在都增加到 50000。现在我们看到以下问题

 myLoadedData1 <- expand.grid(A$id,B$id)  
Error: cannot allocate vector of size 7.1 Gb

请帮助该项目现在有点卡住了,需要一些想法来解决这个问题。请在下面查看我的会话信息

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plyr_1.8.4        dplyr_0.7.7       odbc_1.1.6        data.table_1.11.8

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.17     assertthat_0.2.0 R6_2.2.2         DBI_1.0.0        magrittr_1.5     pillar_1.2.3     rlang_0.2.1      blob_1.1.1      
 [9] bindrcpp_0.2.2   tools_3.5.1      bit64_0.9-7      glue_1.2.0       purrr_0.2.5      bit_1.1-14       hms_0.4.2        yaml_2.1.19     
[17] compiler_3.5.1   pkgconfig_2.0.1  tidyselect_0.2.4 bindr_0.1.1      tibble_1.4.2    

标签: rrstudiodata-science

解决方案


推荐阅读