I have a function call WOE, that return a data fame with 1 row, and 7 columns:
WOE(inData, splitCol, range, tgtCol, adjfac)
Where inData is a data frame, splitCol, tgtCol and adjfac are numeric, range can be a single number or a 2×1 matrix,
e.g. range = 10 or range = c(10, 20)
Now I would like to write a function that when the range is a n-row matrix, then the function will do WOE row by row and return a data frame that have n row and 7 columns.
For now i am using a for loop with cbind:
df <- rbind(df, WOE(inData, splitCol, range[i,], tgtCol, adjfac))
for (i in 2:nrow(range)) {
df <- rbind(df, WOE(inData, splitCol, range[i,], tgtCol, adjfac))
}
But I don’t like for-loop… I want to make it simpler.
So I also have tried to use mapply like this:
mapply(t(WOR, list(inData), list(splitCol), split(range, nrow(range)), list(tgtCol), list(adjfac))
but the above line doesn’t return a data frame as I want, it return a data frame of a lot of lists, which is very difficult for me to do further calculation.
Does anyone have suggestions for me to aggregate my for-loop into less lines?
Thanks!
I often use this idiom when I want precise control over how results from an “apply” operation are combined. This way I don’t have to remember what the rules for automatic simplification are (i.e. sapply or mapply with simplify=TRUE).