Skip to content

Instantly share code, notes, and snippets.

View alienfluid's full-sized avatar

Farhan Ahmed alienfluid

View GitHub Profile
@alienfluid
alienfluid / topnwords.R
Created April 25, 2014 14:22
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=' ')

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 January 31, 2014 22:51
Find the maximum depth of a nested list structure
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 January 31, 2014 22:33
ODBC Error while querying data from MySQL (#julialang)
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 January 2, 2014 19:24
Convert data frame with factors into one with dummy variables for all the factors. Also take care of missing values etc.
## 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)