簡體   English   中英

合並和附加 ffdf 數據幀列表

[英]Merging and appending a list of ffdf dataframes

我想讀取一個包含 CSV 個文件名的矢量作為ffdf數據幀,並將它們組合成一個大ffdf數據幀。 我找到了使用其他r包的解決方案; 然而,我的問題是我的數據(合並)可以達到 40GB,這肯定需要存儲在磁盤上,就像ff package 所做的那樣,而不是在 RAM 中。 據我所知,這里有使用 RAM 存儲的很棒的解決方案

library(ffbase)
library(ff)

# Create list of csv files
csv_files <- list.files(path = input_path,
                        pattern="*.csv",
                        full.names = T)

# my approach so far
# this use fread, and it appears to be consuming RAM 

# Read the files in, assuming comma separator
csv_files_df <- lapply(csv_files, function(x) {
y<-unlist(str_split(x, "[.]"))[1]
    assign(y,
   as.ffdf(fread(x,stringsAsFactors = T)))})

# Combine them
combined_df <- do.call("ffdfappend", lapply(csv_files_df, as.ffdf))

當我嘗試組合它們時,它會引發此錯誤。

> combined_df <- do.call("ffdfappend", lapply(csv_files_df, as.ffdf))
Error in ffdfappend(list(virtual = list(VirtualVmode = c("double", "integer",  : 
  'list' object cannot be coerced to type 'logical'

摘要:我想僅使用ff package 讀取和合並 CSV 文件,而不需要另一個 package 以避免 OOM(內存不足)狀態。

ffdfappend() function 只需要兩個數據 arguments - xy 當您提供列表時,它假設一些數據幀是另一個 arguments 到ffdfappend() 要以您打算的方式使用此 function,您可能需要將其寫在一個循環中,如下所示:

csv_files <- list.files(path = input_path,
                        pattern="*.csv",
                        full.names = T)

# my approach so far
# this use fread, and it appears to be consuming RAM 

read <- function(x) {
  y<-unlist(str_split(x, "[.]"))[1]
  assign(y,
         as.ffdf(fread(x,stringsAsFactors = T)))}

# Read the files in, assuming comma separator
out <- read(csv_files[1])

for(i in 2:length(csv_files)){
  out <- ffdfappend(out, read(csv_files(i)))
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM