Reputation: 121
I'm dealing with a large data that I splitted in chunks so it's manageble by the ram, something like this: (this is an example I have more chunks)
var_1<-all_modell [c(1:150000) ,]; save(var_1,file="~/var_1.Rdata");rm(var_1);
var_2<-all_modell [c(150001:300000),]; save(var_2,file="~/var_2.Rdata");rm(var_2);
var_3<-all_modell [c(300001:450000),]; save(var_3,file="~/var_3.Rdata");rm(var_3);
The idea is that each iteration a chunk is loaded, used to predict and then erased, so the ram is free to process the next chunk:
for (i in 1:n_chunks)
{
name<-sprintf('var_%i',i); path<-sprintf('~/var_%i.Rdata',i)
load(path)
predicted <- predict(Model, newdata =name, type = "prob") #here is the problem
value <- as.numeric(lapply(predicted,"[[",2))
namef <- sprintf('~/predicted%i.Rdata',i)
save(value,file=namef)
rm(list= ls()[!(ls()%in% Model)])
}
What I would like to know is how can I pass newdata=name
where name varies...
I also tried this but it didn't work:
predicted <- predict(Model, parse(text=sprintf(sprintf('newdata=var_%i',i))), type="prob")
Upvotes: 1
Views: 148
Reputation: 49640
It might be a little cleaner to use a seperate environment to hold your chunks instead of using get
(though get
is the simple answer here and is part of FAQ 7.21).
A possible modification of your code:
myenv <- new.env()
for (i in 1:n_chunks) {
name<-sprintf('var_%i',i); path<-sprintf('~/var_%i.Rdata',i)
load(path, env=myenv)
predicted <- predict(Model, newdata =myenv[[name]], type = "prob")
value <- as.numeric(lapply(predicted,"[[",2))
namef <- sprintf('~/predicted%i.Rdata',i)
save(value,file=namef)
rm(list= ls(env=myenv), envir=myenv)
}
Upvotes: 0
Reputation: 179428
Use get()
to do this. Here is a minimal example:
x <- 1:100
x_1 <- x[1:50]
x_2 <- x[51:100]
for(i in 1:2){
var <- sprintf('x_%i',i)
print(sum(get(var)))
}
This results in:
[1] 1275
[1] 3775
See ?get
for more details.
Upvotes: 1