Find the standard deviation of a vector, matrix, or data.frame. In the latter two cases, return the sd of each column. Unlike the sd function, return NA if there are no observations rather than throw an error.

SD(x, na.rm = TRUE)   #deprecated

Arguments

x

a vector, data.frame, or matrix

na.rm

na.rm is assumed to be TRUE

Details

Finds the standard deviation of a vector, matrix, or data.frame. Returns NA if no cases.

Just an adaptation of the stats:sd function to return the functionality found in R < 2.7.0 or R >= 2.8.0 Because this problem seems to have been fixed, SD will be removed eventually.

Value

The standard deviation

Author

William Revelle

Note

Until R 2.7.0, sd would return a NA rather than an error if no cases were observed. SD brings back that functionality. Although unusual, this condition will arise when analyzing data with high rates of missing values. This function will probably be removed as 2.7.0 becomes outdated.

See also

These functions use SD rather than sd: describe.by, skew, kurtosi

Examples

data(attitude)
apply(attitude,2,sd) #all complete
#>     rating complaints privileges   learning     raises   critical    advance 
#>  12.172562  13.314757  12.235430  11.737013  10.397226   9.894908  10.288706 
attitude[,1] <- NA
SD(attitude) #missing a column
#>     rating complaints privileges   learning     raises   critical    advance 
#>         NA  13.314757  12.235430  11.737013  10.397226   9.894908  10.288706 
describe(attitude)
#> Warning: no non-missing arguments to min; returning Inf
#> Warning: no non-missing arguments to max; returning -Inf
#>            vars  n  mean    sd median trimmed   mad min  max range  skew
#> rating        1  0   NaN    NA     NA     NaN    NA Inf -Inf  -Inf    NA
#> complaints    2 30 66.60 13.31   65.0   67.08 14.83  37   90    53 -0.22
#> privileges    3 30 53.13 12.24   51.5   52.75 10.38  30   83    53  0.38
#> learning      4 30 56.37 11.74   56.5   56.58 14.83  34   75    41 -0.05
#> raises        5 30 64.63 10.40   63.5   64.50 11.12  43   88    45  0.20
#> critical      6 30 74.77  9.89   77.5   75.83  7.41  49   92    43 -0.87
#> advance       7 30 42.93 10.29   41.0   41.83  8.90  25   72    47  0.85
#>            kurtosis   se
#> rating           NA   NA
#> complaints    -0.68 2.43
#> privileges    -0.41 2.23
#> learning      -1.22 2.14
#> raises        -0.60 1.90
#> critical       0.17 1.81
#> advance        0.47 1.88