-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdescStats.R
65 lines (65 loc) · 2.2 KB
/
descStats.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
descStats <- function(x, na.rm = TRUE, trim = NULL, skew = FALSE, byrow = FALSE, digits = getOption("digits")) {
fun <- function(x) {
stopifnot(is.numeric(x))
if (na.rm) x <- x[!is.na(x)]
stats <- numeric(8)
n <- length(x)
mean <- sum(x) / n
dev <- x - mean
dev_s2 <- sum(dev^2L)
sd <- sqrt(dev_s2 / (n - 1))
se <- sqrt(sd / n)
half <- (n + 1L) %/% 2L
if (n %% 2L == 1L)
median <- sort(x, partial = half)[half]
else
median <- sum(sort(x, partial = half + 0L:1L)[half + 0L:1L]) / 2L
min <- min(x)
max <- max(x)
range <- max - min
stats <- c(n, mean, se, sd, median, min, max, range)
if (!is.null(trim)) {
if (trim >= 0.5)
trimmed <- median
else {
trimmed <- local({
lo <- floor(n * trim) + 1
hi <- n + 1 - lo
x <- sort(x, partial = unique(c(lo, hi)))[lo:hi]
return(sum(x) / n)
})
}
stats <- append(stats, trimmed, 2)
}
if (skew) {
skewness <- (sum(dev^3L) / n) / (dev_ss / n)^1.5
kurtosis <- n * sum(dev^4L) / (dev_ss^2L)
stats <- c(stats, skewness, kurtosis)
}
return(stats)
}
cn <- colnames(x)
n.vars <- ncol(x)
if (is.data.frame(x)) {
for (i in seq_len(n.vars)) {
if (!is.numeric(x[, i]) || is.factor(x[, i])) {
x <- x[, -i]
warning(paste("Variable \"", colnames(x)[i], "\"was droped from data.frame."))
}
}
}
if (!is.matrix(x))
x <- as.matrix(x)
if (byrow)
x <- t.default(x)
sn <- c("n", "mean", "se", "sd", "median", "min", "max", "range")
if (skew)
sn <- c(stat.nanmes, "skewness", "kurtosis")
if (!is.null(trim))
sn <- append(stat.nanmes, "trimmed", 2)
result <- matrix(numeric(1), ncol = length(sn), nrow = n.vars,
dimnames = list(cn, sn), byrow = TRUE)
for (i in seq_len(n.vars))
result[i, ] <- fun(x[, i])
return(signif(result, digits = digits))
}