I was wondering if there was a built-in function in R that would compute the standard deviation for columns just like colMeans
computes mean
for every column. It would be simple enough to write my own mini function (a compound command that invokes things like apply
with sd
), but I was wondering if there was already something I could use whilst also keeping my code looking clean.
Asked
Active
Viewed 1.4e+01k times
43

Christian Bueno
- 962
- 1
- 10
- 13
-
...there, I would especially recommend sgibb's `colSdColMeans` as a fast implementation. – flodel Aug 05 '13 at 01:18
-
for a numeric matrix, easy enough (solutions below), but how about a solution for a dataframe that only works on numeric columns? Or uses a formula argument to select columns? `colmean(~x1+x2+x3,data=d)`? – Spacedman Aug 05 '13 at 09:52
4 Answers
70
The general idea is to sweep the function across. You have many options, one is apply()
:
R> set.seed(42)
R> M <- matrix(rnorm(40),ncol=4)
R> apply(M, 2, sd)
[1] 0.835449 1.630584 1.156058 1.115269
R>

Dirk Eddelbuettel
- 360,940
- 56
- 644
- 725
-
5In apply(), the second argument, margin, "is a vector giving the subscripts which the function will be applied over." 2 indicates columns – rafaelvalle Nov 28 '14 at 03:11
16
Use colSds
function from matrixStats
library.
library(matrixStats)
set.seed(42)
M <- matrix(rnorm(40),ncol=4)
colSds(M)
[1] 0.8354488 1.6305844 1.1560580 1.1152688

MYaseen208
- 22,666
- 37
- 165
- 309
-
Can you adjust this `colSds` for sums, not only for means? – Léo Léopold Hertz 준영 Nov 11 '16 at 22:21
-
2
-
2
If you want to use it with groups, you can use:
library(plyr)
mydata<-mtcars
ddply(mydata,.(carb),colwise(sd))
carb mpg cyl disp hp drat wt qsec vs am gear
1 1 6.001349 0.9759001 75.90037 19.78215 0.5548702 0.6214499 0.590867 0.0000000 0.5345225 0.5345225
2 2 5.472152 2.0655911 122.50499 43.96413 0.6782568 0.8269761 1.967069 0.5270463 0.5163978 0.7888106
3 3 1.053565 0.0000000 0.00000 0.00000 0.0000000 0.1835756 0.305505 0.0000000 0.0000000 0.0000000
4 4 3.911081 1.0327956 132.06337 62.94972 0.4575102 1.0536001 1.394937 0.4216370 0.4830459 0.6992059
5 6 NA NA NA NA NA NA NA NA NA NA
6 8 NA NA NA NA NA NA NA NA NA NA

Metrics
- 15,172
- 7
- 54
- 83
2
The package fBasics
has a function colStdevs
require('fBasics')
set.seed(123)
colStdevs(matrix(rnorm(1000, mean=10, sd=1), ncol=5))
[1] 0.9431599 0.9959210 0.9648052 1.0246366 1.0351268

user1981275
- 13,002
- 8
- 72
- 101