Angus
Angus

Reputation: 355

Nested For loops are not using next years data

I have a large simulation where I need to move individual orders through time in annual increments. The sample code only shows 10 annual increments of 10 orders, but when I run it it doesn't seem to pick up the information for the following year and I get an error: "Error in if (unused[i] > 200) { : argument is of length zero".

In the code, i is the increment for orders and k is the increment for years. If year=1, it samples from the original data set: dat. At the end of the calculations, it should take what was sampled and create a new data set called newdat, but now with a new and reduced value for unused. If year is > 1, it's supposed to take the newdat dataframe and sample it for the new data and the process is supposed to repeat itself. But, so far it hasn't worked.

dat <- data.frame(cbind(rnorm(30,600,sd=100),rnorm(30,300,sd=50),rnorm(30,200,sd=50),rnorm(30,600,sd=100)))
colnames(dat) <- c("unused","apr","oct","used")

unused <- NULL
deduct <- NULL
carryover <- NULL
used <- NULL
apr <- NULL
oct <- NULL
available <- NULL

#_____________________________________ Sample Data _____________________________________________________
for (i in 1:10) { 
  for (k in 1:10){ 
    subsample <- if(k==1) dat[sample(seq_along(dat[, 1]), size = 1,replace=TRUE), c("unused", "apr","oct","used")]
    subsample <- if(k > 1) newdat[sample(seq_along(newdat[, 1]), size = 1,replace=TRUE), c("unused", "apr","oct","used")]
    unused[i] <-subsample$unused
    apr[i] <- subsample$apr
    oct[i] <- subsample$oct
    used[i] <- subsample$used
#_____________________________________ Use Data _____________________________________________________
    if (unused[i] > 200) {
      deduct[i] <- unused[i]-200
      carryover[i] <- 200
      available[i] <- carryover[i]+apr[i]+oct[i]
    }

    if (unused[i] <= 200) {
      deduct[i] <- 0
      carryover[i] <- unused[i]
      available[i] <- carryover[i]+apr[i]+oct[i]
    }
#_____________________________________ Create DataFrame for following years _________________________

    unused <- available-used
    newdat <- data.frame(cbind(unused,apr,oct,used))
    colnames(newdat) <- c("unused","apr","oct","used")
  }
}

Upvotes: 0

Views: 26

Answers (1)

eastclintw00d
eastclintw00d

Reputation: 2364

The way you defined the if-statements is not correct. Try

dat <- data.frame(cbind(rnorm(30,600,sd=100),rnorm(30,300,sd=50),rnorm(30,200,sd=50),rnorm(30,600,sd=100)))
colnames(dat) <- c("unused","apr","oct","used")

unused <- NULL
deduct <- NULL
carryover <- NULL
used <- NULL
apr <- NULL
oct <- NULL
available <- NULL

#_____________________________________ Sample Data _____________________________________________________
for (i in 1:10) { 
  for (k in 1:10){ 
    if(k==1) {
      subsample <- dat[sample(seq_along(dat[, 1]), size = 1,replace=TRUE), c("unused", "apr","oct","used")]
    } else {
      subsample <- newdat[sample(seq_along(newdat[, 1]), size = 1,replace=TRUE), c("unused", "apr","oct","used")]
    }
    unused[i] <-subsample$unused
    apr[i] <- subsample$apr
    oct[i] <- subsample$oct
    used[i] <- subsample$used
    #_____________________________________ Use Data _____________________________________________________
    if (unused[i] > 200) {
      deduct[i] <- unused[i]-200
      carryover[i] <- 200
      available[i] <- carryover[i]+apr[i]+oct[i]
    }

    if (unused[i] <= 200) {
      deduct[i] <- 0
      carryover[i] <- unused[i]
      available[i] <- carryover[i]+apr[i]+oct[i]
    }
    #_____________________________________ Create DataFrame for following years _________________________

    unused <- available-used
    newdat <- data.frame(cbind(unused,apr,oct,used))
    colnames(newdat) <- c("unused","apr","oct","used")
  }
}

Upvotes: 1

Related Questions