Almost everything in R is done with functions. Below I"m only refering come numeric and character functions that are frequently used in developing or recoding variables.
(To practice working through functions, try the functions sections of this this interactive course.)
Numeric features
Function | Description |
abs(x) | absolute worth |
sqrt(x) | square root |
ceiling(x) | ceiling(3.475) is 4 |
floor(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) | also 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. X substr(x, 2, 4) is "bcd" substr(x, 2, 4) constant expression. If fixed=TRUE climate pattern is a text string. Returns equivalent indices. Grep("A", c("b","A","c"), fixed=TRUE) return 2 |
sub(pattern, replacement, x, ignore.case =FALSE, fixed=FALSE) | Find pattern in x and replace v replacement text. If fixed=FALSE climate pattern is a continual expression. If fixed = T climate pattern is a message string. sub("\\s",".","Hello There") return "Hello.There" |
strsplit(x, split) | Split the aspects of character vector x at split. strsplit("abc", "") returns 3 aspect vector "a","b","c" |
paste(..., sep="") | Concatenate strings after using sep string to seperate them. Paste("x",1:3,sep="") return c("x1","x2" "x3") paste("x",1:3,sep="M") returns c("xM1","xM2" "xM3") paste("Today is", date()) |
toupper(x) | Uppercase |
tolower(x) | Lowercase |
Statistical Probability Functions
The adhering to table defines functions concerned probaility distributions. For arbitrarily number generators below, you can use set.seed(1234) or some other integer to produce reproducible pseudo-random numbers.
Function | Description | |
dnorm(x) | normal density role (by default m=0 sd=1) # plot conventional normal curve x y plot(x, y, type="l", xlab="Normal Deviate", ylab="Density", yaxs="i") | |
pnorm(q) | cumulative common probability because that q (area under the normal curve to the left of q) pnorm(1.96) is 0.975 | |
qnorm(p) | normal quantile. value at the ns percentile the normal distribution qnorm(.9) is 1.28 # 90th percentile | |
rnorm(n, m=0,sd=1) | n random normal deviates with average m and standard deviation sd. #50 random normal variates through mean=50, sd=10 x pbinom(q, size, prob) qbinom(p, size, prob) rbinom(n, size, prob) | binomial distribution where size is the sample size and prob is the probability of a top (pi) # prob the 0 to 5 top of same coin out of 10 flips dbinom(0:5, 10, .5) # prob the 5 or much less heads of same coin out of 10 flips pbinom(5, 10, .5) |
dpois(x, lamda) ppois(q, lamda) qpois(p, lamda) rpois(n, lamda) | poisson distribution with m=std=lamda #probability the 0,1, or 2 occasions with lamda=4 dpois(0:2, 4) # probability of at the very least 3 occasions with lamda=4 1- ppois(2,4) | |
dunif(x, min=0, max=1) punif(q, min=0, max=1) qunif(p, min=0, max=1) runif(n, min=0, max=1) | uniform distribution, follows the very same pattern together the normal circulation above. #10 uniform arbitrarily variates x na.rm=FALSE) | mean of object x # trimmed mean, removing any lacking values and # 5 percent of highest and lowest scores mx # 30th and 84th percentiles the x y indices #indices is c(1, 3, 5, 7, 9) |
rep(x, ntimes) | repeat x n times y # y is c(1, 2, 3, 1, 2, 3) | |
cut(x, n) | divide continuous variable in variable with n level y Robert I. You are watching: How to square in r See more: Solved Which Of The Following Is Not A Property Of Bases? ? Which Of The Following Is Property Of Base Kabacoff, Ph.D. | Sitemap |