| Function | Description |
| dnorm(x) | normal density function (by default m=0 sd=1)# plot standard normal curvex <- pretty(c(-3,3), 30)y <- dnorm(x) plot(x, y, type='l', xlab="Normal Deviate", ylab="Density", yaxs="i") |
| pnorm(q) | cumulative normal probability for q(area under the normal curve to the left of q)pnorm(1.96) is 0.975 |
| qnorm(p) | normal quantile. value at the p percentile of normal distributionqnorm(.9) is 1.28 # 90th percentile |
| rnorm(n, m=0, sd=1) | n random normal deviates with mean mand standard deviation sd. #50 random normal variates with mean=50, sd=10x <- rnorm(50, m=50, sd=10) |
| dbinom(x, size, prob)pbinom(x, size, prob)qbinom(p, size, prob)rbinom(n, size, prob) | binomial distribution where size is the sample sizeand prob is the probability of a heads (pi)# prob of 0 to 5 heads of fair coin out of 10 flipsdbinom(0:5, 10, .5)# prob of 5 or less heads of fair coin out of 10 flipspbinom(5, 10, .5) |
| dpois(x,lamda)ppois(x,lamda)qpois(x,lamda)rpois(x,lamda) | poisson distribution with m=std=lamda#probability of 0,1, or 2 events with lamda=4dpois(0:2, 4)# probability of at least 3 events with lamda=41- ppois(2,4) |
| dunif(x, min=0, max=1)punif(x, min=0, max=1)qunif(x, min=0, max=1)runif(x, min=0, max=1) | uniform distribution, follows the same patternas the normal distribution above.#10 uniform random variates x <- runif(10) |
Almost everything in R is done through functions. Here I'm only refering to numeric and character functions that are commonly used in creating or recoding variables.
(To practice working with functions, take the Intermediate R Course.)
Numeric Functions
Character Functions
Statistical Probability Functions
The following table describes functions related to probaility distributions. For random number generators below, you can use set.seed(1234) or some other integer to create reproducible pseudo-random numbers.
Other Statistical Functions
Other useful statistical functions are provided in the following table. Each has the option na.rm to strip missing values before calculations. Otherwise the presence of missing values will lead to a missing result. Object can be a numeric vector or data frame.
Other Useful Functions
Note that while the examples on this page apply functions to individual variables, many can be applied to vectors and matrices as well.