typestar

csv_summary.R in R

A complete script: read a CSV, summarize each column, print a report.

args <- commandArgs(trailingOnly = TRUE)
if (length(args) != 2) {
  stop("usage: Rscript csv_summary.R FILE COLUMN")
}

data <- read.csv(args[1])
column <- args[2]
if (!column %in% names(data)) {
  stop(paste("no column named", column))
}

values <- data[[column]]
values <- values[!is.na(values)]

cat("count: ", length(values), "\n")
cat("mean:  ", round(mean(values), 3), "\n")
cat("median:", round(median(values), 3), "\n")
cat("sd:    ", round(sd(values), 3), "\n")

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
466
Tokens
144
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 86 seconds.

Type this snippet

Step 1 of 1 in Encore, step 20 of 20 in Language basics.

← Previous