histelheim
histelheim

Reputation: 5088

What does this R expression do?

sp_full_in is matrix:

   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
1  0 1 1 1 1 2 2 2 1  1  1  1  1  2  1  1  1  1  1  1  2
2  1 0 1 1 1 1 1 1 1  1  1  2  1  1  1  1  1  1  1  1  1
3  2 2 0 2 2 2 2 2 2  1  1  2  2  2  1  2  1  1  1  2  1
4  1 2 1 0 2 2 2 1 2  1  1  1  2  2  1  2  1  1  2  2  1
5  2 2 2 2 0 2 2 2 2  1  1  2  1  2  1  2  1  1  1  2  2
6  2 1 1 1 1 0 1 1 1  2  2  2  2  2  1  2  1  2  2  1  1
7  2 1 1 2 1 1 0 1 1  2  1  1  2  1  1  2  1  1  1  2  1
8  1 2 1 1 1 2 2 0 1  1  1  2  2  2  1  2  1  1  2  1  1
9  2 2 1 2 1 1 2 2 0  1  1  2  1  2  1  2  1  1  2  2  2
10 2 2 1 1 1 2 2 1 1  0  2  2  2  2  1  1  1  1  1  2  2
11 2 2 1 1 1 2 1 1 1  1  0  2  1  2  1  2  1  1  1  1  2
12 1 2 1 1 2 1 1 2 1  1  1  0  2  2  1  2  1  2  1  1  1
13 2 2 2 2 1 3 2 2 2  1  1  3  0  2  1  2  2  1  2  2  2
14 2 2 1 2 1 2 1 2 1  2  2  2  1  0  1  2  1  1  1  1  1
15 2 2 2 2 2 2 2 2 2  1  1  2  2  1  0  2  1  1  1  1  2
16 1 2 2 1 1 2 2 2 1  1  2  2  2  2  1  0  1  1  2  1  2
17 2 2 1 1 1 1 1 2 1  1  1  1  2  2  1  2  0  2  2  1  1
18 1 1 1 1 1 2 1 1 1  1  1  2  1  1  1  1  2  0  1  1  1
19 2 2 1 2 1 2 2 2 2  1  1  2  2  2  1  2  1  1  0  2  2
20 2 2 1 1 1 2 2 2 2  1  2  2  2  2  1  2  1  1  1  0  1
21 1 1 1 1 1 1 1 1 1  2  2  1  2  1  1  2  1  1  2  1  0

mean(sp_full_in[which(sp_full_in != Inf)])

produces the result [1] 1.38322

I'm not quite sure I understand what this does, but the way I read it is: for every cell in sp_full_in, check if it is not infinite, if so, return the output 1, then average all the outputs. Is that correct? If not, how should it be ready?

Upvotes: 1

Views: 93

Answers (3)

Gregor Thomas
Gregor Thomas

Reputation: 145765

The other answers are good at explaining why you get the mean of all the finite entries in the matrix, but it's worth noting that in this case the which does nothing. I used to have the bad habit of over-using which as well.

> a <- matrix(rnorm(4), nrow = 2)
> a
           [,1]       [,2]
[1,]  0.5049551 -0.7844590
[2,] -1.7170087 -0.8509076
> a[which(a != Inf)]
[1]  0.5049551 -1.7170087 -0.7844590 -0.8509076
> a[a != Inf]
[1]  0.5049551 -1.7170087 -0.7844590 -0.8509076
> a[1] <- Inf
> a
          [,1]       [,2]
[1,]       Inf -0.7844590
[2,] -1.717009 -0.8509076
> a[which(a != Inf)]
[1] -1.7170087 -0.7844590 -0.8509076
## Similarly if there was an Infinite value
> a[a != Inf]
[1] -1.7170087 -0.7844590 -0.8509076

And, while we're at it, we should also mention the function is.finite which is often preferable to != Inf. is.finite will return FALSE on Inf, -Inf, NA and NaN.

Upvotes: 1

IRTFM
IRTFM

Reputation: 263331

which(sp_full_in != Inf) returns a vector of integers (and only one of them is 1). That vector of integers is then handed to "[" as indices into sp_full_in and returns all the values of sp_full_in as a vector passed to the mean function.

It is a good idea to learn to read R expressions from the "inside out". Find the innermost function call and mentally evaluate it, in this case sp_full_in != Inf,. That returns a logical matrix of all TRUE's that gets passed to which(), and since there is no 'arr.ind' argument, it returns an atomic vector of indices.

Upvotes: 4

Haoyan
Haoyan

Reputation: 51

No, but you are close, when which is applied to a matrix, it checks every cell of the matrix against the condition,here it is Not Inf. Return the indices of all cells satisfying the conditions,then, according to your code, output the value of the cell according to the returned indices and finally calculate mean of those.

Upvotes: 0

Related Questions