Example R programs and commands 3. Range, mean deviation, variance, diversity # All lines preceded by the "#" character are my comments. # All other left-justified lines are my input. # All other indented lines are the R program output. # Input data z <- c(1.1, 2.0, 1.5, 2.1, 1.9, 1.8, 3.5, 3.6, 3.7) # Value range range(z) [1] 1.1 3.7 # Deviation z-mean(z) [1] -1.2555556 -0.3555556 -0.8555556 -0.2555556 -0.4555556 -0.5555556 1.1444444 [8] 1.2444444 1.3444444 # Absolute deviation abs(z-mean(z)) [1] 1.2555556 0.3555556 0.8555556 0.2555556 0.4555556 0.5555556 1.1444444 [8] 1.2444444 1.3444444 # Mean deviation mean(abs(z-mean(z))) [1] 0.8296296 # Variance var(z) [1] 0.9602778 # Standard deviation sd(z) [1] 0.9799376 # Coefficient of variance sd(z)/mean(z) [1] 0.4160113 # Shannon diversity f <- c(54, 13, 27, 2) n <- sum(f) # total number in the population k <- length(f) # number of categories # Maximum Shannon diversity for this number of categories: HSmax <- log(k); HSmax # Shannon diversity of these frequencies: HS <- (n*log(n) - sum(f*log(f)))/n; HS # Shannon evenness: HS/HSmax # Brillouin diversity f <- c(54, 13, 27, 2) # frequency table n <- sum(f) # total number in the population k <- length(f) # number of categories # Use integer quotient c and remainder d from n=ck+d: c <- n %/% k; d <- n %% k; # Maximum Brillouin diversity for this number of categories: Hmax <- (lfactorial(n) - (k-d)*lfactorial(c) - d*lfactorial(c+1))/n; Hmax # Brillouin diversity of these frequencies: H <- (sum(log(1:n))-sum(lfactorial(f)))/n; H # Brillouin evenness: H/Hmax # NOTE: The lfactorial() function in R computes the natural logarithm # of the factorial of its argument, which is much more sensible for # large values. If you use common (base 10) logarithms, then you must # multiply the numbers by log(10) ~ 2.3036 to get the natural (base e) # logarithms used in Brillouin's index.