I have 5000 rows of up to 2000 values separated by ";" which are currently stuffed into a single column. I would like to separate them, add them and throw away the original column. Problem: I max out memory quickly. Currently using more than 60GB RAM. What is a good way to deal with this?
So far I have:
Option 1: Dplyr
#Not every line has the same number of values
MaxNumSemiColon=2001 #Max amount
df %>%
separate(dose_close, sep=";",
remove = TRUE,
into = paste0("dummy",1: MaxNumSemiColon),
convert = TRUE,
fill = "right")%>%
rowwise() %>%
mutate(dose_close = sum(c_across(dummy1:paste0("dummy",numSemiColon)),na.rm = TRUE))%>%
select(-contains("dummy"))
Option 2: Data.table
setDT(df)[, paste0("dose_close", 1:numSemiColon) := tstrsplit(value, ";", type.convert = TRUE, fixed = TRUE)]
temp<-temp[,-1]
temp<-temp%>% rowSums() %>% as.data.frame() %>% set_colnames("dose_close")
df$dose_close<-NULL
df<-cbind(df,temp) #Bind dose_close
NB: I saw data.table::fread has sep2=";" which would be ideal but does anyone know when it might be available?