Having trouble with this. The driver of this problem is that I am doing some analysis of large data sets that I need to reshape using tidyr::pivot_longer(). However, it throws a memory allocation error if I try to pivot the entire dataset(~20m rows). I have tried to work around this by splitting the data into ten pieces and looping over each one. However, for this to work, I need to clear memory after each iteration of the loop.
The problem comes when I try to use rm() to remove a specific list object from memory. The format df_pivots[[i]]
throws an error, but if I turn it into a string, it breaks the iteration of the loop. Reprex below:
library(dplyr)
library(tidyr)
library(data.table)
df_pivots <- list()
for(i in 1:10){
df_pivots[[i]] <- data.frame(a= rnorm(1000), b= rnorm(1000))
df_pivots[[i]] <- pivot_longer(df_pivots[[i]], cols = c(a,b),
names_to = 'Original_Column',
values_to = 'Value')
fwrite(df_pivots[[i]], paste('df_pivots_',
0, i, '.csv', sep = '' ))
rm(df_pivots[[i]])
gc()
}
#> Error in rm(df_pivots[[i]]): ... must contain names or character strings
Created on 2022-09-15 with reprex v2.0.2