standard deviation for each element in matrix

Hello!
I've got a matrix organized from my dataset. It has 15 rows and 15 columns.

Now I need to calculate standard deviation for EACH value (not for each row or each column but for each cell) with cell value being >1 and <10

And I'm a little bit stuck here.
I tried to use apply and lapply function but I don't know how to include these >1 and <10 parameters.

Any advice? Thank u!

standard deviation is an aggregate statistic that has meaning by its relationship to multiple measures of a population of some size ; on the other hand - a single observation, i.e. a population of 1 member only can't have a meaningful standard deviation, as what it is observed to be is what it is.
unless perhaps your matrix is 3dimensional, so the 3rd dimension represents multiple measures from the subjects defined by their positions in the other 2 dimensions; but you said 15 rows and 15 columns; so I'm supposing not.

2 Likes

Continuing nirgrahamuk's discussion,

Consider the set of numbers {3}. The mean of those numbers is 3. Standard deviation equals the square root of the average of the squared deviations of each number in the set from the mean. The average deviation of each number in that set from the mean is 0. So the resulting standard deviation is 0. This is not a particularly meaningful number.

@nirgrahamuk & @fcas80 have to say about SD. To subset within 2:9

m <- matrix(sample(1:20,15),sample(1:20,15), nrow = 15,ncol = 15)
m
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#>  [2,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#>  [3,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#>  [4,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#>  [5,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#>  [6,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#>  [7,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#>  [8,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#>  [9,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#> [10,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#> [11,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#> [12,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#> [13,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#> [14,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#> [15,]   16   12    9   20   11    1   10   13   18     2     5    19    14
#>       [,14] [,15]
#>  [1,]    17     7
#>  [2,]    17     7
#>  [3,]    17     7
#>  [4,]    17     7
#>  [5,]    17     7
#>  [6,]    17     7
#>  [7,]    17     7
#>  [8,]    17     7
#>  [9,]    17     7
#> [10,]    17     7
#> [11,]    17     7
#> [12,]    17     7
#> [13,]    17     7
#> [14,]    17     7
#> [15,]    17     7
# elements will be coming out, zapping the matrix structure, so
# use a vector
v <- c(m)
v
#>   [1] 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 12 12 12 12 12 12 12 12 12 12
#>  [26] 12 12 12 12 12  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9 20 20 20 20 20
#>  [51] 20 20 20 20 20 20 20 20 20 20 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
#>  [76]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 10 10 10 10 10 10 10 10 10 10
#> [101] 10 10 10 10 10 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 18 18 18 18 18
#> [126] 18 18 18 18 18 18 18 18 18 18  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2
#> [151]  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5 19 19 19 19 19 19 19 19 19 19
#> [176] 19 19 19 19 19 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 17 17 17 17 17
#> [201] 17 17 17 17 17 17 17 17 17 17  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7
v[v > 1 & v < 10]
#>  [1] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5 5 5
#> [39] 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

Created on 2022-11-24 by the reprex package (v2.0.1)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.