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.
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
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
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>
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
Source: Stackoverflow.com