Skip to content

Instantly share code, notes, and snippets.

@prasoonsharma
prasoonsharma / Solve a hard Sudoku problem using R
Created May 20, 2011 08:29
Solve a hard Sudoku problem using R
# -----------------------------
# SUDOKU
# -----------------------------
# Solve a Sudoku problem in seconds
# create a text file under c:\ drive called "hard sudoku problem.txt" with following text (without # symbol) and then run the code below
#85---24--
#72------9
#--4------
#---1-7--2
@prasoonsharma
prasoonsharma / gist:830809
Created February 17, 2011 02:13
Stata dynamic variables example
foreach i in A B C D {
forval n=1990/2000 {
local m = 'n'-1
# create new columns from existing ones on-the-fly
generate pop'i''n' = pop'i''m' * (1 + trend'n')
}
}
@prasoonsharma
prasoonsharma / gist:818724
Created February 9, 2011 16:09
Stata and R
# 1) Stata command:
use growth_and_uptake_assumptions.dta
# Corresponding R command:
load(file="my_data.RData")
attach(my_data)
# ------------------------------------------------
# 2) Stata command:
Player Club Pos Price Pts
A Carroll Newcastle STR 2.0 84
A Cole Chelsea DEF 7.0 92
C Tevez Man City STR 7.0 104
D Berbatov Man Utd STR 6.0 95
F Malouda Chelsea MID 5.5 106
G Bale Tottenham DEF 5.0 122
J Hart Man City GK 4.0 86
P Zabaleta Man City DEF 3.0 71
S Nasri Arsenal MID 4.0 121
@prasoonsharma
prasoonsharma / gist:733497
Created December 8, 2010 16:20
R code for Fantasy Football (soccer)
# Load lpsolve library
# install.packages('lpSolve')
library('lpSolve')
# Read player data (name, position, price, points)
players <- read.csv("http://spreadsheets.google.com/pub?key=0AvZWqry9LOMddDY3OVNLeDVNWTEtTEczbkRtUmx5SFE&hl=en&single=true&gid=0&output=csv", sep=",", header=TRUE)
# OBJECTIVE FUNCTION: Points of all players
f.obj <- players$Pts
@prasoonsharma
prasoonsharma / gist:725167
Created December 2, 2010 11:47
Basic plots in R
# DOT PLOT
x <- c(1:100)
y <- x*x
plot(x, y)
# LINE PLOT
x <- c(1:100)
y <- x*x
plot(x, y, type="l")
@prasoonsharma
prasoonsharma / gist:725143
Created December 2, 2010 11:09
Loops and conditions in R
# LOOPS IN R
# For
i <- 1
for(i in 1:10){
print(paste("value of iterator: ", i))
}
# While
i <- 1
@prasoonsharma
prasoonsharma / gist:717903
Created November 27, 2010 13:42
Data Frame operations in R
# DATA FRAME OPERATIONS IN R
# Create data frame
# A dataset is ~ table (list of vectors)
id <- c(1,2,3)
name <- c("John", "Kirk", "AJ")
age <- c(21,27,18)
employees <- data.frame(ID=id, Name=name, Age=age)
employees
@prasoonsharma
prasoonsharma / gist:717887
Created November 27, 2010 13:23
Curriculum for Introduction to R
CURRICULUM FOR INTRODUCTION TO R (DRAFT)
Day 1:
a) Install R
b) Use R as a calculator
Day 2:
a) Getting help
b) Use variables in R
c) Understand atomic data types in R
@prasoonsharma
prasoonsharma / gist:717849
Created November 27, 2010 12:21
Getting help on R
# LOCAL HELP
# R offers great help articles with the install
# Just type ? and the function if you know the function name
?sqrt # Pay special attention to function definition and take a look at the examples
# Type ?? and your search keyword to search a keyword in R help documentation
??maximum