rawdata <- matrix(c(1,1,1,3,3,1,
2,2,4,6,6,0,
10,10,20,30,30,0,
3,3,2,1,1,0,
0,0,0,20,0,0), ncol = 6, byrow = TRUE)
colnames(rawdata) <- paste("species",toupper(letters[1:6]), sep = "_")
rawdata
species_A species_B species_C species_D species_E species_F
[1,] 1 1 1 3 3 1
[2,] 2 2 4 6 6 0
[3,] 10 10 20 30 30 0
[4,] 3 3 2 1 1 0
[5,] 0 0 0 20 0 0
# Row sums
rowSums(rawdata)
[1] 10 20 100 10 20
apply(rawdata, 1, sum)
[1] 10 20 100 10 20
# Max values
apply(rawdata, 1, max)
[1] 3 6 30 3 20
# Sums
apply(rawdata, 2, sum)
species_A species_B species_C species_D species_E species_F
16 16 27 60 40 1
colSums(rawdata)
species_A species_B species_C species_D species_E species_F
16 16 27 60 40 1
# Max
apply(rawdata, 2, max)
species_A species_B species_C species_D species_E species_F
10 10 20 30 30 1
Useful for when you have a wide spread in data values
Ir is important that you add 1 to values to account for zeros log10(x+1)
logdata <- apply(rawdata , c(1,2), function(x) log10(x + 1))
library(tidyverse)
hemlock <- read_csv("https://raw.githubusercontent.com/chrischizinski/SNR_R_Group/master/data/hemlock_cover.csv")
hemlock$logTsuga<- log10(hemlock$Tsuga.canadensis +1) # log transform
glimpse(hemlock)
Observations: 98
Variables: 3
$ Site <int> 1001, 1002, 1003, 1004, 1005, 1006, 1011, 1102, 1108, 1110, 1112, 1201, 1203, 1206, 1305, 1306, 1307, 1...
$ Tsuga.canadensis <dbl> 0.1, 34.0, 42.0, 21.0, 37.0, 18.0, 9.0, 0.0, 0.0, 0.0, 43.0, 8.0, 0.0, 4.0, 45.0, 25.0, 0.0, 0.0, 0.0, ...
$ logTsuga <dbl> 0.04139269, 1.54406804, 1.63346846, 1.34242268, 1.57978360, 1.27875360, 1.00000000, 0.00000000, 0.00000...
ggplot(data = hemlock) +
geom_histogram(aes(Tsuga.canadensis), binwidth = 5, colour = "black", fill = "dodgerblue") +
coord_cartesian(ylim = c(0, 30), expand = FALSE) +
theme_bw()
ggplot(data = hemlock) +
geom_histogram(aes(logTsuga), bins = 30, colour = "black", fill = "red") +
coord_cartesian(ylim = c(0, 30), expand = FALSE) +
theme_bw()
pwr_trans <- function(x, trans){
x <- ifelse(x>0,x^(1/trans),0)
return(x)
}
pwr_trans(25,2)
[1] 5
pwr_trans(0,2)
[1] 0
newdata <- data.frame(x = 0:100,
cubic = pwr_trans(x=0:100, trans = 3),
power10 = pwr_trans(x=0:100, trans = 10))
head(newdata)
ggplot(data = newdata) +
geom_line(aes(x = x, y = cubic), size = 1, colour = "blue") +
geom_line(aes(x = x, y = power10), size = 1, colour = "red") +
labs(y = "Value") +
coord_cartesian(xlim = c(0,100.5), ylim = c(0,5), expand = F)+
theme_classic()
NA
library(vegan)
Loading required package: permute
Loading required package: lattice
This is vegan 2.4-3
decostand(rawdata, method = "pa")
species_A species_B species_C species_D species_E species_F
[1,] 1 1 1 1 1 1
[2,] 1 1 1 1 1 0
[3,] 1 1 1 1 1 0
[4,] 1 1 1 1 1 0
[5,] 0 0 0 1 0 0
attr(,"decostand")
[1] "pa"
Please NOTE: The arcsine is asinine: the analysis of proportions in ecology
ttl_species <- apply(rawdata, 1, sum)
rowprop_data <- rawdata / ttl_species
rowprop_data
species_A species_B species_C species_D species_E species_F
[1,] 0.1 0.1 0.1 0.3 0.3 0.1
[2,] 0.1 0.1 0.2 0.3 0.3 0.0
[3,] 0.1 0.1 0.2 0.3 0.3 0.0
[4,] 0.3 0.3 0.2 0.1 0.1 0.0
[5,] 0.0 0.0 0.0 1.0 0.0 0.0
decostand(rawdata, margin = 1, method = "total")
species_A species_B species_C species_D species_E species_F
[1,] 0.1 0.1 0.1 0.3 0.3 0.1
[2,] 0.1 0.1 0.2 0.3 0.3 0.0
[3,] 0.1 0.1 0.2 0.3 0.3 0.0
[4,] 0.3 0.3 0.2 0.1 0.1 0.0
[5,] 0.0 0.0 0.0 1.0 0.0 0.0
attr(,"decostand")
[1] "total"
colprop_data <- rawdata %*% diag(1/apply(rawdata,2,sum))