I have two sets of numbers A and B. I would like to do a bootstrap analysis where I calculate the percent difference between the values in each group but only for cases where a value in group A > B.
I've tried to code below but all the values I get are 0. Does anyone have any advice on how to do this?
A <- c(13,5,8,1,23,4,7,2,14,27)
B <- c(5,7,0.5,10, 1,3, 0.7,2,0.8,4)
percentage_difference <- function(value, value_two) {
(value - value_two) / value
}
n <- 10000
Per_Dif <- numeric(n)
for (i in 1:n) {
if(A > B){Per_Dif[i] <- percentage_difference(sample(A, replace = T), sample(B, replace = T))}
}
CodePudding user response:
Try
do.call(c,lapply(1:n, function(boot){
a <- sample(A, replace = T)
b <- sample(B, replace = T)
percentage_difference(a,b)[a>b]
}))