1

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)
user438383
  • 5,716
  • 8
  • 28
  • 43
Max
  • 21
  • 2
  • What to you want for an output in return? One Gini-value for each split? – Julian Apr 29 '22 at 11:00
  • @julian yes I am trying to get Gini Value for each split. – Max Apr 29 '22 at 11:13
  • 1
    Hi, have you tried something like: `purrr::map(.x = 1:200, .f = compute_gini, df = ***, var = ***, minsplit = ***)`? With `***` being your actual inputs for these variables. – Paul Apr 29 '22 at 11:14
  • @Marcuswas minsplit is just a constant 1. – Max Apr 29 '22 at 11:16
  • Could you please make your example reproducible? See https://stackoverflow.com/help/minimal-reproducible-example and https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. We do not know what your function is neither what the objects and the expected ouput are. – Paul Apr 29 '22 at 11:34
  • 1
    @paul `purrr::map(.x = 1:200, .f = compute_gini, df = ***, var = ***, minsplit = ***)` This code worked well and solved my problem! Thankssssssss! – Max Apr 29 '22 at 12:00

2 Answers2

1

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
Paul
  • 2,850
  • 1
  • 12
  • 37
0

One idea could be to combine the function with a for-loop: for (i in 1:200) {compute_gini(df, var, i, minsplit)}

Marcuswas
  • 98
  • 6