I have a dataframe and my goal here is to take all possible combinations of the rank column and for each rank combination apply a function that will use the volume_metric and kpi_metric values. The resulting output would be a matrix just like the cor()
function provides except with each rank combinations p-values.
Basically I want to take the first row values of volume_metric & kpi_metric and then the second row values of volume_metric & kpi_metric and apply the zTest
function to them. Then 1->3, 1->4, etc.
rank <- c(1, 2, 3, 4, 5, 6, 7, 8)
volume_metric <- c(12321, 12321, 1232121, 4343, 14333, 52323, 234532, 2322)
kpi_metric <- c(12, 32, 111, 334, 653, 343, 232, 212)
# The df
df <- tibble(rank, volume_metric, kpi_metric)
# A tibble: 8 x 3
rank volume_metric kpi_metric
<dbl> <dbl> <dbl>
1 1 12321 12
2 2 12321 32
3 3 1232121 111
4 4 4343 334
5 5 14333 653
6 6 52323 343
7 7 234532 232
8 8 2322 212
# z-test fucntion
zTest <- function(volume1, volume2, kpi1, kpi2) {
z_test <- prop.test(
x=c(kpi1, kpi2),
n=c(volume1, volume2),
alternative = "greater",
conf.level = 0.95,
correct = FALSE
)
p_value <- z_test$p.value
return(p_value)
}
So far I have been able to get all of the rank combinations using
possible_combinations <- combn(nrow(df), 2)
which will provide a matrix with all of the combos (the rank will always be the same as now(df)
).
I tried to loop through that matrix and then subset the df
but that resulted in a never ending loop