In the absence of a reproducible example, the following uses an example from the {Metrics}
package documentation to construct your dataframe dp
. Augment as appropriate.
Further you need to provide parameters to your function. In this case we supply the data frame dp
(which you call in your function).
Lastly, replicate()
returns an array/matrix. We reformat this into a "long" format and then coerce it to a data frame.
library(Metrics)
# simulate the data -----------------------------------------
actual <- c(1.1, 1.9, 3.0, 4.4, 5.0, 5.6)
predicted <- c(0.9, 1.8, 2.5, 4.5, 5.0, 6.2)
dp <- data.frame(
true_norm = actual
, dp_threshold_norm = predicted
)
# make function work -----------------------------------------
getValue <- function(dp) { # we define a parameter dp for the function
mae <- mae(dp$true_norm, dp$dp_threshold_norm)
rmse <- rmse(dp$true_norm, dp$dp_threshold_norm)
per_metrics <- c(mae,rmse)
return(per_metrics) # return value
}
# apply function multiple times with replicate()
# check this to understand the returned data format
replicate(n = 10, expr = getValue(dp))
# result ---------------------------------------------
## store in variable
result <- replicate(n = 10, expr = getValue(dp))
## coerce to "long data frame" - here we set ncol 2 for 2 variables
result <- matrix(result, ncol = 2)
## coerce to data frame
result <- as.data.frame.array(result)
This yields:
result
V1 V2
1 0.2500000 0.2500000
2 0.3341656 0.3341656
3 0.2500000 0.2500000
4 0.3341656 0.3341656
5 0.2500000 0.2500000
6 0.3341656 0.3341656
7 0.2500000 0.2500000
8 0.3341656 0.3341656
9 0.2500000 0.2500000
10 0.3341656 0.3341656
You can now rename the columns as required.