I've got this function to be applied later over 3D-arrays f3 <- function(x) {-sum((x / sum(x)) * log2(x / sum(x)))}
and I'm trying to apply the condition that, if the second part of the function is -Inf
(i.e. log2(x/sum(x)=-Inf
), then this part will be 0
(i.e. log2(x/sum(x))=0)
. I'm trying to nest an if statement inside the function (see below) but not working for now.
f3 <- function(x) {
-sum((x / sum(x)) * log2(x / sum(x)))
if((log2(x/sum(x)))==-Inf) {
log2(x/sum(x))==0
}
}
I use this function over an 3D-array of 1000 24x24-matrices (a) to try this I,m only using the first matrix of the array a[,,1]
apply (a[,,1],2,f3)
Any little indication would be welcome. Thanks in advance.