【问题标题】:Using dplyr in r with large dataset (4 million rows)在具有大型数据集(400 万行)的 r 中使用 dplyr
【发布时间】:2020-12-26 08:15:09
【问题描述】:

我正在使用 dplyr 进行一些数据操作,并使用我的巨大数据 (b) 框架。 我已经能够成功处理较小的数据子集。我想我的问题在于我的数据框的大小。

我的数据框有 400 万行和 34 列。

我的代码如下:

df<-b %>%
  group_by(Id) %>%
  mutate(numberoflead = n(),#lead sayısı
         lastcreateddateoflead=max(CreatedDate),#last date of lead
         firstcreateddateoflead=min(CreatedDate),#first date of lead
         lastcloseddate=max(Kapanma.tarihi....),#last closed date of kapanm tarihi
         yas=as.Date(lastcloseddate)-as.Date(firstcreateddateoflead),#yas
         leadduration=as.Date(lastcreateddateoflead)-as.Date(firstcreateddateoflead)) %>%#lead duration
  inner_join(b %>% 
               select(Id, CreatedDate, lasttouch = Lead_DataSource__c),
             by = c("Id" = "Id", "lastcreateddateoflead" = "CreatedDate")) %>% #lasttouch
  inner_join(b %>% 
               select(Id, CreatedDate, firsttouch = Lead_DataSource__c),
             by = c("Id" = "Id", "firstcreateddateoflead" = "CreatedDate")) %>%  #firsttouch
  inner_join(b %>% 
               select(Id, Kapanma.tarihi...., laststagestatus = StageName),#laststagestatus
             by = c("Id" = "Id", "lastcloseddate" = "Kapanma.tarihi...."))

它在我的数据帧的较小子集上运行良好,但是当我将上面的代码运行到我的完整数据帧时, 它运行了很长时间并最终崩溃。我认为问题可能出在我的数据框的 400 万行

有人对如何做到这一点有任何建议吗?非常感谢您的帮助!

【问题讨论】:

  • 试试data.table,即setDT(b)[, c('numberoflead', 'lastcreateddateoflead') := .(.N, max(CreatedDate)), Id]
  • 还可以查看dtplyr(数据表后端到dplyr)和dbplyr(SQL 数据库后端到dplyr)
  • @BenBolker,我已经尝试过使用 dtplyr 但现在我得到了这个错误;错误:无法分配大小为 17.5 Mb 的向量。有什么想法吗?
  • 这意味着您仍然遇到内存限制。你有多少内存?您可能需要内存不足的解决方案(例如 dbplyr 或查看 cran.r-project.org/web/views/HighPerformanceComputing.html

标签: r dplyr large-data


【解决方案1】:

我最近遇到了类似大小的代码的类似问题。我认为您的问题是 R 内存空间的大小。您可以在 R 编辑器中检查全局环境上方的容量。我的内存因大数据量而过载,然后程序经常崩溃。

我的解决方案是编写两段单独的代码。首先,我将所有数据集合并到一个文件中,并以

结尾
saveRDS(file, file = "filename.Rds") # save as one object to save work space in working directory as R data file

然后关闭文件,手动清除 R 内存(点击“释放未使用的 R 内存”)并启动一个新代码,我在其中加载了之前创建的文件

setwd("PathWhereTheFileIsSaved") # set working directory
complete <- readRDS(file = "filename.Rds") # load previously in code 1 created data 

之后,我的代码可以正常工作而不会导致内存过载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-20
    • 2017-11-13
    • 2015-10-20
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    相关资源
    最近更新 更多