This file contains hidden or 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
################################################# | |
## 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 |
This file contains hidden or 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
################################################# | |
## Probability Functions ## | |
################################################# | |
######################## | |
# Normal Distribution | |
# - density (dnorm) | |
# - distribution (pnorm) | |
# - quantile (qnorm) | |
# - random deviate generation (rnorm) |
This file contains hidden or 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
########################################### | |
## Character Function ## | |
########################################### | |
# Number of characters | |
x <- c("ab", "cde", "fghij") | |
a <- length(x) | |
b <- nchar(x[3]) # returns 5 | |
# substring |
This file contains hidden or 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
################################################### | |
### 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) |
This file contains hidden or 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
########################################### | |
## Other Useful Functions ## | |
########################################### | |
# remove a self-written function from memory | |
rm('knn') | |
# length of object | |
x <- length(c(2,5,6,7)) | |
x |
This file contains hidden or 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
################################################### | |
# ----------------Flow Control -------------------# | |
# For Looping | |
# WHILE Looping | |
# IF-ELSE Conditional Execution | |
# IFELSE Conditional Execution | |
# SWITCH Conditional Execution | |
################################################### | |
# For Looping |
This file contains hidden or 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
##################################################### | |
# ---------- 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 { |
This file contains hidden or 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
##################################################### | |
# -------- Aggregation and Restructuring -----------# | |
##################################################### | |
############ | |
#Transpose | |
############ | |
cars <- mtcars[1:5, 1:4] | |
t(cars) |
This file contains hidden or 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
#-----------------------------------------------------------------------------# | |
#-----------------------------------------------------------------------------# | |
# R in Action - Basic Graphs | |
# - Bar Plots | |
# - Pie charts | |
# - Fan plots | |
# - Histogram | |
# - Kernel Density Plot | |
# - Box plot | |
# - Dot plot |
This file contains hidden or 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
########################################################################## | |
# -----------------------------------------------------------------------# | |
# -------------------- Similarity Metrics (CF) --------------------------# | |
# ---------------------Author: Shaohua Zhang ---------------------------# | |
########################################################################## | |
# sample data 1 | |
Mov1 <- c(4,4,3,4,2) | |
Mov2 <- c(NA,2,NA,4,1) |