0

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.

shasha
  • 19
  • 5
  • keep in mind that `=` (single equal sign) is an assignment operator equivalent to `<- `. For comparisons you should use `==` (double equal sign) – Jilber Urbina Mar 08 '18 at 17:46
  • Thanks, I`ve edited the question properly. I have tried with '==' and not working. – shasha Mar 08 '18 at 17:48
  • Make your question clearer by providing a [minimal reproducible example](https://stackoverflow.com/q/5963269/1315767) – Jilber Urbina Mar 08 '18 at 19:49
  • Here is the initial question and how was finally solved. https://stackoverflow.com/q/49116372/9057078 – shasha Mar 12 '18 at 16:49

2 Answers2

0

I believe what you're looking for can be accomplished like so:

f3 <- function(x) {
  if(log2(x/sum(x)) == -Inf) {
    result <- 0
  } else {
    result <- -sum((x / sum(x)) * log2(x / sum(x)))
  }
  return(result)
}
93i7hdjb
  • 1,136
  • 1
  • 9
  • 15
  • Thanks. Ive got these errors:Error in if (log2(x/sum(x)) == -Inf) { : missing value where TRUE/FALSE needed In addition: Warning messages: 1: In if (log2(x/sum(x)) == -Inf) { : the condition has length > 1 and only the first element will be used – shasha Mar 08 '18 at 18:01
  • Can you share what you're inputting into `f3()` when you get these errors? – 93i7hdjb Mar 08 '18 at 18:03
  • apply (a[,,1], 2, f3) , where a is just the first 24x24-matrix of an array of 1000. To try Im just using 1 matrix, Then I extend it to all the array – shasha Mar 08 '18 at 18:07
  • I see. Change the `if` statement to `if(any(log2(x/sum(x)) == -Inf))` – 93i7hdjb Mar 08 '18 at 18:13
  • The issue is: an array cannot evaluate to `-Inf`, only one of its components can. Adding the `any()` function to your if statement will check to see if any item in the array evaluates to `-Inf`, and if so, will return `TRUE` – 93i7hdjb Mar 08 '18 at 18:29
  • To try I'm using just 1 matrix. I have tried any(), but it does not seem the solution... – shasha Mar 08 '18 at 18:38
  • Ok, Well I'm afraid to help further, I'd need to see sample data of what you're feeding in to `f3()` and what you're desired output is. – 93i7hdjb Mar 08 '18 at 18:40
  • Here is the initial question and how was finally solved. https://stackoverflow.com/q/49116372/9057078 – shasha Mar 12 '18 at 16:51
0

maybe this is useful for you. Just minor changes:

f3 <- function(x) {
  denom <- log2(x / sum(x))
  if(denom==-Inf|is.nan(denom)) { denom <- 0}
  -sum((x / sum(x)) * denom)
}

NOTE: keep in mind that = (single equal sign) is an assignment operator equivalent to <- . For logical comparisons you should use == (double equal sign)

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • Thanks. Ive got these errors:Error in if (log2(x/sum(x)) == -Inf) { : missing value where TRUE/FALSE needed In addition: Warning messages: 1: In if (log2(x/sum(x)) == -Inf) { : the condition has length > 1 and only the first element will be used – shasha Mar 08 '18 at 18:02
  • @shasha I think your error is produced when x takes value o, try to add an extra condition in the `if` statement to handle NaN when x=0 correctly (see update). Also remember to provide a [minimal reproducible example](https://stackoverflow.com/q/5963269/1315767) so that we can help you better. – Jilber Urbina Mar 08 '18 at 19:54
  • That's ok. This is the problem... I'll add reproducible examples in the future. For now I think I have to try alternative solutions – shasha Mar 09 '18 at 08:58
  • Here is the initial question and how was finally solved https://stackoverflow.com/q/49116372/9057078 – shasha Mar 12 '18 at 16:47