Skip to content

Instantly share code, notes, and snippets.

@hadley hadley/topwords.R
Created Apr 25, 2014

Embed
What would you like to do?
#!/usr/bin/env Rscript
n <- as.integer(commandArgs(trailingOnly = TRUE))
lines <- readLines(file("stdin"))
words <- tolower(unlist(strsplit(lines, "\\W+")))
counts <- sort(table(words), decreasing = TRUE)
counts_n <- counts[1:n]
cat(sprintf("%8d %s\n", counts_n, names(counts_n)), sep = "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.