I am wondering if it's possible to avoid looping. I want to make the function faster. The function is to find how many times a particular value is lower in the overall data. For example:
Value <- c(0,10,5,1,0,0,11,0,0,7,3,2,5) # length =13
The 1st value is 0 and 0 is NOT lower in any of the values. Therefore the value I would like to return from the function would be 0.
Something like this:
sum(Values < 0)
0
The 2nd value is 10:
sum(Values < 10)
11
I make a function something like below:
fun_num <- function(Value){
gg <- vector()
for (i in seq_along(Value)){
x <- Value[i]
y <- sum(Value < x)
gg[i] <- y
}
return(gg)
}
Applying the function:
fun_num(Value)
[1] 0 11 8 5 0 0 12 0 0 10 7 6 8
Just wondering, is there a way to speed this function up using sapply
and avoid using for loop?