Integrating a Loop Around My Function for Testing

I need to wrap this function in a for loop (or lapply) and pass in the values using a CSV file. I feel like I might be close to the solution, but I'm not experienced enough in R to know how to do that.

This is my function:

run(calculate_fnscore, list("MonthsEmployed" = 1,
                                  "MonthlyRent" = 0,
                                  "CO_Age" = NULL,
                                  "LN_ialenofres" = NULL,
                                  "NewVehInd" = boolean_to_bit(FALSE)))

The list values I have to put in by hand and I need to test 10,000 values. That's not practical. I need to put this in some kind of for loop and pass in the values from a CSV file or another way to make this more automated. Any help is appreciated.

Here's the basic scaffolding of what a potential for loop solution could look like:

x <- data.frame(MonthsEmployed = 1:3,
                MonthlyRent = 1:3,
                CO_Age = 1:3,
                LN_ialenofres = 1:3,
                NewVehInd = logical(3))

results <- numeric(length = nrow(x))

for (i in seq_len(nrow(x))) {
  results[i] <- run(calculate_fnscore, list("MonthsEmployed" = x$MonthsEmployed[i],
                                  "MonthlyRent" = x$MonthlyRent[i],
                                  "CO_Age" = x$CO_Age[i],
                                  "LN_ialenofres" = x$LN_ialenofres[i],
                                  "NewVehInd" = x$NewVehInd[i]))
}

A few questions:

  1. Where are the functions run, calculate_fnscore, and boolean_to_bit coming from? Did you write these or are they from a package? I assume that the list contains arguments to the function calculate_fnscore, and I'm unsure of the advantage of passing these via run.
  2. Does your CSV file use NULL for missing values? If yes, you could convert these to NA by setting the argument na.strings = "NULL" for the function read.csv.
  3. Would it be possible to convert your question to a reproducible example?
2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.