Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
Created April 17, 2012 14:31
Show Gist options
  • Save geofferyzh/2406352 to your computer and use it in GitHub Desktop.
Save geofferyzh/2406352 to your computer and use it in GitHub Desktop.
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
# sum(x) -- sum(c(1,2,3,4)) returns 10
# diff(x,lag=n) -- lagged difference
# min(x)
# max(x)
# scale(x,center=TRUE, scale=TrUE)
####################
# Standardizing Data
####################
# 1 - standardize to 0 mean and 1 sd
newdata <- scale(mydata)
# 2 - standardize to arbitrary mean and sd
newdata <- scale(mydta)*SD + M
# 3 - standardize a column/variable
newdata <- transform(mydata, myvar = scale(myvar)*10+50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment