Skip to content

Instantly share code, notes, and snippets.

View topnwords.R
#!/usr/bin/env Rscript
library(tm)
num.words <- as.integer(commandArgs(trailingOnly = TRUE))
f <- file("stdin")
input.lines <- readLines(f)
close(f)
full.text <- tolower(paste(input.lines, collapse = " "))
freqs <- sort(termFreq(PlainTextDocument(full.text), control=list(wordLengths= c(1,Inf))), decreasing=T)[1:num.words]
for (i in 1:num.words) {
cat(freqs[i], names(freqs)[i], "\n", sep=' ')
View keybase.md

Keybase proof

I hereby claim:

  • I am alienfluid on github.
  • I am farhan (https://keybase.io/farhan) on keybase.
  • I have a public key whose fingerprint is 1A95 F6A8 4B52 8F81 3D09 DD0F 064B 8FA3 0210 0988

To claim this, I am signing this object:

@alienfluid
alienfluid / nested_lists.R
Created Jan 31, 2014
Find the maximum depth of a nested list structure
View nested_lists.R
a = list(list("a", "b"), list(list(list(list(list("c")))), list(list("d"))), 1)
is_list = function (x) {
if (class(x) == "list") {
1 + max(sapply(x, is_list))
} else {
0
}
}
@alienfluid
alienfluid / gist:8744619
Created Jan 31, 2014
ODBC Error while querying data from MySQL (#julialang)
View gist:8744619
julia> ODBC.connect("mysql-prod2")
ODBC Connection Object
----------------------
Connection Data Source: mysql-prod2
mysql-prod2 Connection Number: 1
Contains resultset? No
julia> results = query("select * FROM company LIMIT 100")
ERROR: no method unsafe_copy!(Ptr{UTF8String},Ptr{Uint32},Int64)
in ODBCCopy! at /Users/fahmed/.julia/ODBC/src/backend.jl:129
@alienfluid
alienfluid / gist:8224955
Created Jan 2, 2014
Convert data frame with factors into one with dummy variables for all the factors. Also take care of missing values etc.
View gist:8224955
## build a nice design matrix
## formula can be a vector of column names or a standard formula
design <- function(formula, response, data){
if(is.character(formula)){
data <- data[,formula]
formula <- ~.
}
if(is.null(dim(data))) data <- as.matrix(data)
if(is.null(rownames(data))) rownames(data) <- 1:nrow(data)
response <- c(response)