內建函數
https://www.statmethods.net/management/functions.html
Numeric Functions
Function | Description |
---|---|
abs(x) | absolute value |
sqrt(x) | square root |
ceiling(x) | floor(3.475) is 3 |
trunc(x) | trunc(5.99) is 5 |
round(x, digits=n) | round(3.475, digits=2) is 3.48 |
signif(x, digits=n) | signif(3.475, digits=2) is 3.5 |
cos(x) | sin(x), tan(x), ,acos(x), cosh(x), acosh(x), etc. |
log(x) | natural logarithm |
log10(x) | common logarithm |
exp(x) | e^x |
Character Functions
Function | Description |
---|---|
substr(x, start=n1, stop=n2) | Extract or replace substrings in a character vector. |
grep(pattern, x , ignore.case=FALSE, fixed=FALSE) | Search for pattern in x. If fixed =FALSE then pattern is a regular expression. |
sub(pattern, replacement, x, ignore.case =FALSE, fixed=FALSE) | Find pattern in x and replace with replacement text. |
strsplit(x, split) | Split the elements of character vector x at split. |
paste(..., sep="") | Concatenate strings after using sep string to seperate them. |
toupper(x) | Uppercase |
Lowercase | Lowercase |
Examples
#1 substr
x <- "abcdef"
substr(x, 2, 4) is "bcd"
substr(x, 2, 4) <- "22222" is "a222ef"
#2 grep
grep("A", c("b","A","c"), fixed=TRUE)
#returns 2
#3 sub
sub("\\s",".","Hello There") #\\s Matches any white-space character
#returns "Hello.There"
#4 strsplit
strsplit("abc", "") #returns 3 element vector "a","b","c"
#5 paste
paste("x",1:3,sep="") #returns c("x1","x2" "x3")
paste("x",1:3,sep="M") #returns c("xM1","xM2" "xM3")
paste("Today is", date())
Statistical Functions
Function | Description |
---|---|
mean(x, trim=0,na.rm=FALSE) | mean of object x |
sd(x) | standard deviation |
median(x) | median |
quantile(x, probs) | quantiles where x is the numeric vector whose quantiles are desired and probs is a numeric vector with probabilities in [0,1]. |
range(x) | range |
sum(x) | sum |
diff(x, lag=1) | differences |
min(x) | minimum |
max(x) | maximum |
scale(x, center=TRUE, scale=TRUE) | |
var(x, y = NULL, na.rm = FALSE, use) | Variance |
cov(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman")) | Covariance |
cor(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman")) | Correlation |
//Correlation, Variance and Covariance (Matrices) https://stat.ethz.ch/R-manual/R-devel/library/stats/html/cor.html
Examples
#1 mean
# trimmed mean, removing any missing values and
# 5 percent of highest and lowest scores
mx <- mean(x,trim=.05,na.rm=TRUE)
#2 quantile
quantileedian(x) median
# 30th and 84th percentiles of x
y <- quantile(x, c(.3,.84))
Data Generation Functions
Function | Description |
---|---|
seq(from , to, by) | generate a sequence |
rep(x, ntimes) | repeat x n times |
cut(x, n) | divide continuous variable in factor with n levels |
Examples
#1 seq
indices <- seq(1,10,2)
#indices is c(1, 3, 5, 7, 9)
#2 rep
y <- rep(1:3, 2)
# y is c(1, 2, 3, 1, 2, 3)
#3 cut
y <- cut(x, 5)
Statistical Probability Functions
Function | Description |
---|---|
dnorm(x) | normal density function (by default m=0 sd=1) |
pnorm(q) | cumulative normal probability for q |
qnorm(p) | normal quantile. |
rnorm(n, m=0,sd=1) | qnorm(.9) is 1.28 # 90th percentile rnorm(n, m=0,sd=1) n random normal deviates with mean m and standard deviation sd. |