This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LOOPS IN R | |
# For | |
i <- 1 | |
for(i in 1:10){ | |
print(paste("value of iterator: ", i)) | |
} | |
# While | |
i <- 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Stata command: | |
use growth_and_uptake_assumptions.dta | |
# Corresponding R command: | |
load(file="my_data.RData") | |
attach(my_data) | |
# ------------------------------------------------ | |
# 2) Stata command: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ----------------------------- | |
# 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 |
OlderNewer