Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
summarize: A script to generate summarize statistics from input
#!/usr/bin/Rscript
#
# Quickly generate some basic descriptive
# statistics from stdin
#
library('optparse', quietly=TRUE)
option_list <- list(make_option(c("-H", "--header"), action="store_true", default=FALSE,
help="Use first line of input as headers."),
make_option(c("-r", "--rownames"), action="store", default=NULL,
help="Column to use as row names."),
make_option(c("-s", "--sep"), action="store", default="",
help="Column seperator. Defaults to white space"))
parser <- OptionParser(usage = "%prog [OPTIONS] [FILE]", option_list=option_list)
arguments <- parse_args(parser, positional_arguments=TRUE)
opts <- arguments$options
args <- arguments$args
input_file <- if (is.na(args[1])) {
file('stdin')
} else {
args[1]
}
input <- read.table(input_file, header=opts$header, sep=opts$sep, row.names=opts$rownames)
summary(input)
write("\nCovariance Matrix:", stdout())
var(input)
write("\nColumn Sums:", stdout())
colSums(input)
write("\nNumber of Rows:", stdout())
nrow(input)
write("\nFirst 6 Rows:", stdout())
head(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment