Skip to content

Instantly share code, notes, and snippets.

View mhkeller's full-sized avatar

Michael Keller mhkeller

View GitHub Profile
@mhkeller
mhkeller / New dataframe from columns R
Created April 24, 2012 15:44
Create a new R dataframe from a subset of columns from another df
unique(df[, c("a", "c")])
or
df <- subset(df, select = c(a,c))
or to drop
df <- subset(df, select = -c(a,c))
@mhkeller
mhkeller / Renaming R Column Names
Created April 24, 2012 15:49
Rename columns headers in a dataframe in R
names(df) <- c("column1","column2")
or by name
names(df)[names( df)=="Los_Angeles"]<-"LA"
df <- read.csv("data.csv")
df$new_column1<- ifelse(df$c3 == "A",1,0)
df$new_column2<- ifelse(df$c4 == "B",1,0)
df$new_column3 <-ifelse(df$c5 == "C" & df$c6 = D, 1, 0)
via @BrianAbelson
@mhkeller
mhkeller / R Concatenating
Created May 4, 2012 18:20
Concatenate for R
df2 <- paste (df1$col1, df1$col2, sep = "_")
@mhkeller
mhkeller / Sorting dataframes in R
Created May 4, 2012 18:36
Order dataframe by columns
descending
df[with(df, order(-a)), ]
or
df[order(-df$col1),]
ascending
@mhkeller
mhkeller / save SVG in R, ggplot2
Created May 7, 2012 19:31
Save files in ggplot2
ggsave(ratings, file="ratings.svg")
Symbol Meaning Example
%d day as a number (0-31) 01-31
%a abbreviated weekday, Mon
%A unabbreviated weekday, Monday
%m month (00-12), 00-12
%b abbreviated month, Jan
%B unabbreviated month, January
%y 2-digit year
%Y 4-digit year
^(?!http).+
\.*
@mhkeller
mhkeller / gist:3120449
Created July 16, 2012 04:06
Explode a columns properties into new columns with a binary value
# Nice for then doing aggregate functions
for(t in unique(df$colToExplode)) {
a[paste("",t,sep="")] <- ifelse(df$colToExplode==t,1,0)
}
@mhkeller
mhkeller / get_scores.R
Created August 7, 2012 18:10 — forked from drewconway/get_scores.R
Function returns quater scores from Wikipedia Super Bown pages
# Function returns quater scores from Wikipedia Super Bown pages
get.scores<-function(numeral) {
# Base URL for Wikipedia
wp.url<-getURL(paste("http://en.wikipedia.org/wiki/Super_Bowl_",numeral,sep=""))
wp.data<-htmlTreeParse(wp.url, useInternalNodes=TRUE)
score.html<-getNodeSet(wp.data,"//table[@style='background-color:transparent;']")
score.table<-readHTMLTable(score.html[[1]])
score.table<-transform(score.table, SB=numeral)
return(score.table)
}