1
2
3
4
5
#

R - Statistical Functions

 RStudio Code Examples Function x = 1:10 > x = 1:10 > x [1] 1 2 3 4 5 6 7 8 9 10 Creates a sequence x from 1 to 10 length(x) > x = c(1, 4, 7, 3, 9) > length(x) [1] 5 Number of values in dataset x max(x) > x = c(1, 10, 20, 50, 100) > max(x) [1] 100 Largest number in dataset x min(x) > x = c(1, 10, 20, 50, 100) > min(x) [1] 1 Smallest number in dataset x mean(x) > x = c(1, 10, 20, 50, 100) > mean(x) [1] 36.2 Mean median (x) > x = c(1, 10, 20, 50, 100) > median(x) [1] 20 Median sd(x) > x = c(1, 10, 20, 50, 100) > sd(x) [1] 40.15221 Standard Deviation var(x) > x = c(1, 10, 20, 50, 100) > var(x) [1] 1612.2 Variance range(x) > x = c(1, 10, 20, 50, 100) > range(x) [1] 1 100 Range cor(x, y) > x = c(1, 10, 20, 50, 100) > y = c(1, 2, 3, 4, 5) > cor(x, y) [1] 0.9372113 Correlation sum(x) > x = c(1, 10, 20, 50, 100) > sum(x) [1] 181 Sum of numbers in dataset x cumsum(x) > x = c(1, 10, 20, 50, 100) > cumsum(x) [1] 1 11 31 81 181 Cumulative sum of numbers in x prod(x) > x = c(1, 3, 5, 7, 9) > prod(x) [1] 945 Product of numbers in dataset x cumprod(x) > x = c(1, 3, 5, 7, 9) > cumprod(x) [1] 1 3 15 105 945 Cumulative product of numbers in x diff(x) > x = c(1, 3, 5, 7, 9) > diff(x) [1] 2 2 2 2 Differences in numbers in dataset x summary(x) > x = c(1, 3, 5, 7, 9) > summary(x) Min. 1st Qu. Median Mean 3rd Qu. Max. 1 3 5 5 7 9  Summary of dataset: minimum, first quartile, median, mean, third quartile, maximum fivenum(x) > x = c(1, 3, 5, 7, 9) > fivenum(x) [1] 1 3 5 7 9 Minimum, first quartile, median, third quartile, maximum of dataset quantile(x, 0.25) > x = c(1, 3, 5, 7, 9) > quantile(x, 0.75) 75% 7  Calculates numbers associated with certain percentiles (e.g. 25%) IQR(x) > x = c(1, 3, 5, 7, 9) > IQR(x) [1] 4 Interquartile range of x weighted.mean(x, w) > x = c(1, 3, 5, 7, 9) > w = c(5, 8, 3, 6, 10) > weighted.mean(x, w) [1] 5.5 Weighted mean where x is set of values and w is set of weights for each number in x rank(x) > x = c(10, 4, 6, 2, 8, 20) > rank(x) [1] 5 2 3 1 4 6 Rank of each number in dataset x union(x, y) > x = c(10, 4, 6, 2, 8, 20) > y = c(10, 2, 12, 14, 18, 36) > union(x, y) [1] 10 4 6 2 8 20 12 14 18 36 Union of 2 datasets x and y intersect(x, y) > x = c(10, 4, 6, 2, 8, 20) > y = c(10, 2, 12, 14, 18, 36) > intersect(x, y) [1] 10 2 Intersection of 2 datasets x and y