-
Notifications
You must be signed in to change notification settings - Fork 1
/
freq_comb.R
80 lines (67 loc) · 2.14 KB
/
freq_comb.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
##' Frequency of species combinations
##'
##' The `freq_comb()` function extracts the combinational frequency of species.
##' `freq_comb()` is minor and features in the vignette to assist users
##' visualize their data.
##'
##' @param data data.frame with the frequency of species combinations.
##' @template output_type_param
##' @template output_dim_max_param
##'
##' @return The function returns an object of class data.frame.
##' @author James L. Tsakalos
##' @seealso [`comspat()`]
##' @examples
##'
##' # Training data (see package vignette S.9)
##' s9 <- data.frame(
##' Species = c("A", "D", "B", "B", "E", "C", "B", "A"),
##' Y = c(1, 1, 2, 2, 2, 3, 3, 3),
##' X = c(2, 3, 1, 2, 2, 1, 1, 3)
##' )
##'
##' # create data.frame counting the species combinations
##' freq_comb(
##' data = s9,
##' type = "Grid",
##' dim_max = 3
##' )
##'
##' @export
freq_comb <- function(data, type, dim_max) {
output <- NULL
if (type == "Grid") data[, 2] <- paste(data$X, data$Y)
for (i in unique(data$X)) {
temp <- data[data$X == i, ]
if (type == "Grid" & nrow(temp) > 0) {
temp <- data.frame(
"combinations" =
aggregate(temp$Species[order(temp$Species)] ~ ., temp, toString)[3],
"Plot" = i)
}
if (type == "Transect" & nrow(temp) > 0) {
temp <- data.frame(
"combinations" =
aggregate(temp$Species[order(temp$Species)] ~., temp, toString)[2],
"Plot" = i)
}
output <- rbind(output, temp)
}
if (type == "Grid") {
potential <- expand.grid(c(1:dim_max), c(1:dim_max))
potential <- length(paste(potential$Var1, potential$Var2))
realised <- length(unique(paste(data$X, data$Y)))
diff <- potential - realised
} else {
potential <- length(c(1:dim_max))
realised <- length(unique(data$X))
diff <- potential - realised
}
names(output) <- c("combinations", "plot")
output <- rbind(data.frame("combinations" = rep("Empty", diff),
"plot" = rep(999, diff)), output)
output <- as.data.frame(table(output[1]))
output[, 2] <- (output$Freq / sum(output$Freq)) * 100
colnames(output)[1] <- "combinations"
return(output)
}