Nicolai Jørgensen - 9 months ago 36

R Question

I have an out from a lower.tri output and wnat to do the mean on the first column.

How to do that?

Answer

An easy way to do it (and also get any column mean) would be to (instead of using `lower.tri`

directly on the matrix) do the following:

```
#Assume a 5x5 matrix
a <- matrix(1:25, nrow=5)
#set upper.tri and the diag to NA
#so what you are left with is just the lower.tri values in a matrix
a[upper.tri(a)] <- diag(a) <- NA
```

Then it is easy to do:

```
colMeans(a, na.rm=TRUE)
[1] 3.5 9.0 14.5 20.0 NaN
```

And get all of them. `colMeans(a, na.rm=TRUE)[1]`

will just give you the first.

Source (Stackoverflow)