-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_lengths.R
70 lines (59 loc) · 1.98 KB
/
convert_lengths.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
#' Convert fish length
#'
#' Converts fish length data using the length conversion table. Returns an arrow dataset.
#' This function is only needed to convert Suisun survey data.
#'
#' @param data An arrow dataset of fish data. See `open_fish()`
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#'
#' @return data_conv An arrow dataset with converted lengths
#' @export
#'
#' @examples
#' \dontrun{
#' library(dplyr)
#'
#' fish <- open_fish() %>%
#' filter(Taxa %in% c("Dorosoma petenense", "Morone saxatilis", "Spirinchus thaleichthys"))
#'
#' fish_conv <- convert_lengths(fish) %>%
#' collect()
#' }
#'
convert_lengths <- function(data){
if (any(!c("Taxa", "Length") %in% names(data))){
stop("Input data must have Taxa and Length column names")
}
if (!("Source" %in% names(data))){
surv <- open_survey() %>%
dplyr::select("SampleID", "Source")
data_prep <- dplyr::left_join(data, surv)
} else data_prep <- data
sources<-data_prep%>%
dplyr::distinct(.data$Source)%>%
dplyr::collect()%>%
dplyr::pull("Source")
if (!("Suisun" %in% sources)){
warning("No Suisun data found in input data. This function only operates on Suisun data.")
return(data)
}
l <- open_length_conv()
sp <- l%>%
dplyr::distinct(.data$Species)%>%
dplyr::collect()%>%
dplyr::pull("Species")
data_f <- data_prep %>%
dplyr::left_join(l, by = c("Taxa" = "Species")) %>%
dplyr::mutate(Length = ifelse(.data$Source == "Suisun" & .data$Taxa %in% sp,
.data$Intercept + .data$Slope * .data$Length,
.data$Length)) %>%
dplyr::select(-"Intercept", -"Slope")
# if source wasn't in input data drop it
if (!("Source" %in% names(data))){
data_f <- data_f %>%
dplyr::select(-"Source")
}
return(data_f)
}