lundi 31 juillet 2017

Overwriting previous iterations of if statement in R

I am VERY new to R and am having a very difficult time getting an answer to this, so I finally caved to post - so apologies ahead of time.

I am using a genetic algorithm to optimize the shape of an object, and want to gather the intermediate steps for prototyping. The package I am using genalg, allows a monitor function to track the data which I can print just fine. But I'd like to stash it in a data frame for other uses and keep watching it overwrite the other iterations. Here's my code for the monitor function:

    monitor <- function(obj){

    #Make empty data frame in which to store data
    resultlist <- data.frame(matrix(nrow = 200, ncol = 10, byrow = TRUE))

    #If statement evaluating each iteration of algorithm
    if (obj$iter > 0){

    #Put results into list corresponding to number of iteration
    resultlist[,obj$iter] <- obj$population[which.min(obj$best),]}

    #Make data frame available at global level for prototyping, output, etc.
    resultlistOutput <<- resultlist}

I know this works in a for loop with no issues based on searches, so I must be doing something wrong or the if syntax is not capable of this?

Sincere thanks in advance for your time.

Aucun commentaire:

Enregistrer un commentaire