forked from ices-taf/SmartDotsReport_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities_data.R
More file actions
45 lines (35 loc) · 1.26 KB
/
Copy pathutilities_data.R
File metadata and controls
45 lines (35 loc) · 1.26 KB
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
# Determine modal age and CV ##################################################
# For each sample the cv and modal age are calculated.
# If one age is more frequent than others, that age is chosen as modal age.
# If no age is more frequent, then the average of all ages are chosen or
# if two (or more) ages are equally frequent then the age read by the most
# expericed reader will be chosen as modal age.
# WHich method to use is set in the ma_method variable.
# If the modal age is 0 the CV is set to 0 as well.
add_modalage <- function(ad, ma_method) {
# ages by fish
out <-
ad %>%
select(FishID, reader, age) %>%
spread(key = reader, value = age)
ages <- out %>% select(-FishID)
# Determine modal age depending on ma_method
out$modal_age <-
if (ma_method == "Mean") {
stop ("mean not implemented yet")
} else if (ma_method == "Mode") {
apply(ages, 1,
function(x) {
if (!is.null(Mode(x))) {
Mode(x)
} else {
trunc(mean(x, na.rm = TRUE) + 0.5)
}
})
}
# calculate CV
out$cv <- apply(ages, 1, cv)
out$cv[is.na(out$modal_age) | out$modal_age == 0] <- NA
# merge CV and modal age to data
right_join(ad, out, by = "FishID")
}