mardi 22 janvier 2019

R - Simplifying code to loop through records and sample factors

I am looking for a way to simplify my code (and make it more efficient). My code loops through records in a table. If the record does not have an age of 4, it randomly samples a record from the pool with the same age and applies the factor. Also, age increases by 1. When the age reaches 4, it stops.

My given data:

set.seed(777)

pool <- data.frame(ID = 1:10,
                   Age = sample(1:4, 10, replace = TRUE),
                   Amt = round(runif(10, 0, 10)*100,0),
                   Factor = round(runif(10, 0.5, 2), 2))

tgt <- pool[sample(nrow(pool), 2, TRUE), 1:3]

The code loops through the records of tgt and applies a random factor until the age reaches 4.

repeat{
  for (i in 1:nrow(tgt)) {
    age.i <- tgt[i, 'Age']
    if(age.i < 4) {
      pool.i <- subset(pool, Age == age.i)
      factor.i <- pool.i[sample(nrow(pool.i), 1), 'Factor']
      tgt <- tgt %>%
        mutate(Age = ifelse(ID == tgt[i, 'ID'], Age + 1, Age),
               Amt = ifelse(ID == tgt[i, 'ID'], Amt * factor.i, Amt))
    }
  }
  if(min(tgt$Age) == 4) {
    break
  }
}

In this loop, it: (1) selects a record, (2) samples a record from pool with the same age, (3) applies the factor to the amount and increments age by 1. This continues until all records in tgt have an age of 4.

With my given code and see, the results are

ID Age      Amt
 9   4  352.000
 8   4 2101.784




Aucun commentaire:

Enregistrer un commentaire