Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created March 1, 2012 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewheiss/1947224 to your computer and use it in GitHub Desktop.
Save andrewheiss/1947224 to your computer and use it in GitHub Desktop.
Cool R summary
coolSummary <- function(x) {
if (class(x) == "data.frame") {
x.min <- apply(x, 2, min, na.rm=TRUE)
x.max <- apply(x, 2, max, na.rm=TRUE)
x.sd <- apply(x, 2, sd, na.rm=TRUE)
x.mean <- as.numeric(lapply(x, mean, na.rm=TRUE))
x.n <- apply(!is.na(x), 2, sum)
x.na <- apply(is.na(x), 2, sum)
row.names <- colnames(x)
} else {
x.min <- min(x, na.rm=TRUE)
x.max <- max(x, na.rm=TRUE)
x.sd <- sd(x, na.rm=TRUE)
x.mean <- mean(x, na.rm=TRUE)
x.n <- sum(!is.na(x))
x.na <- sum(is.na(x))
row.names <- deparse(substitute(x))
}
results <- data.frame(x.mean, x.sd, x.min, x.max, x.n, x.na)
colnames(results) <- c("Mean", "Std Dev", "Min", "Max", "N", "NAs")
rownames(results) <- row.names
results
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment