I have a function "compute_gini" with 4 inputs.
compute_gini(df, var, split, minsplit)
I want it to run let's say 200 times but each time it runs, the input value for split should get increased by 1. Is it possible to run this function multiple times with the purrr:map function? For example...
compute_gini(df , var , 1 , minsplit)
compute_gini(df , var , 2 , minsplit)
compute_gini(df , var , 3 , minsplit)
compute_gini(df , var , 4 , minsplit)
compute_gini(df , var , 5 , minsplit)
.....
compute_gini(df , var , 200 , minsplit)
update:
I tried this function below and it worked!
purrr::map(.x = 1:200, .f = compute_gini, df = penguins_sub, var = bm, minsplit = 1)
CodePudding user response:
One solution could be to map
over the vector 1:200
for ex. Note that you can use seq
to make this vector in another way.
Then, this should work:
fn <- function(a, b, c, d) {
a b c d
}
purrr::map(.x = 1:200, .f = fn, a = 1, c = 1, d = 1)
#> [[1]]
#> [1] 4
...
#> [[200]]
#> [1] 203
This is the same output (but in a list) as:
fn(a = 1, b = 1, c = 1, d = 1)
#> [1] 4
fn(a = 1, b = 2, c = 1, d = 1)
#> [1] 5
fn(a = 1, b = 3, c = 1, d = 1)
#> [1] 6
CodePudding user response:
One idea could be to combine the function with a for-loop: for (i in 1:200) {compute_gini(df, var, i, minsplit)}