Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
geofferyzh / gist:2473057
Created April 23, 2012 18:56
RinAction - R Graph - Basic Graphs
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
# R in Action - Basic Graphs
# - Bar Plots
# - Pie charts
# - Fan plots
# - Histogram
# - Kernel Density Plot
# - Box plot
# - Dot plot
@geofferyzh
geofferyzh / gist:2408580
Created April 17, 2012 19:49
RinAction - R Data Manipulation - Reshaping Data
#####################################################
# -------- Aggregation and Restructuring -----------#
#####################################################
############
#Transpose
############
cars <- mtcars[1:5, 1:4]
t(cars)
@geofferyzh
geofferyzh / gist:2406915
Created April 17, 2012 15:37
RinAction - R Functions - User-Written Functions
#####################################################
# ---------- User Written Function -----------------#
# - objects in the function are local to the function
# - object returned can be any data type
#####################################################
mystats <- function(x, parametric=TRUE, print=FALSE) {
if (parametric) {
center <- mean(x); spread <- sd(x)
} else {
@geofferyzh
geofferyzh / gist:2406809
Created April 17, 2012 15:29
RinAction - R Flow Control
###################################################
# ----------------Flow Control -------------------#
# For Looping
# WHILE Looping
# IF-ELSE Conditional Execution
# IFELSE Conditional Execution
# SWITCH Conditional Execution
###################################################
# For Looping
@geofferyzh
geofferyzh / gist:2406427
Created April 17, 2012 14:44
RinAction - R Functions - Useful Functions
###########################################
## Other Useful Functions ##
###########################################
# remove a self-written function from memory
rm('knn')
# length of object
x <- length(c(2,5,6,7))
x
@geofferyzh
geofferyzh / gist:2406417
Created April 17, 2012 14:41
RinAction - R Functions - Applying Functions to R Objects
###################################################
### Apply Functions to Matrices and Data Frames ###
###################################################
# One of the interesting features of R functions is that they can be applied to a variety of
# data objects (scalars, vectors, matrices, arrays, and data frames).
b <- c(1.243, 5.654, 2.99)
round(b)
@geofferyzh
geofferyzh / gist:2406383
Created April 17, 2012 14:37
RinAction - R Functions - Character Functions
###########################################
## Character Function ##
###########################################
# Number of characters
x <- c("ab", "cde", "fghij")
a <- length(x)
b <- nchar(x[3]) # returns 5
# substring
@geofferyzh
geofferyzh / gist:2406368
Created April 17, 2012 14:34
RinAction - R Functions - Probability Functions
#################################################
## Probability Functions ##
#################################################
########################
# Normal Distribution
# - density (dnorm)
# - distribution (pnorm)
# - quantile (qnorm)
# - random deviate generation (rnorm)
@geofferyzh
geofferyzh / gist:2406352
Created April 17, 2012 14:31
RinAction - R Functions - Statistical Functions
#################################################
## Basic Stats Functions ##
#################################################
# quantile(x,probs) -- Quantiles where x is the numeric vector where
# quantiles are desired
# -- y <- quantile(x, c(0.3, 0.84))
# range(x) -- Range
# -- range(c(1,2,3,4)) returns c(1,4)
# -- diff(range(c(1,2,3,4))) returns 3
@geofferyzh
geofferyzh / gist:2406341
Created April 17, 2012 14:30
RinAction - R Functions - Math Functions
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
# R in Action - Advanced Data Management #
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
#################################################
## Basic Math Functions ##
#################################################