-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogbook_highlights.R
85 lines (70 loc) · 3.72 KB
/
logbook_highlights.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
81
82
83
84
85
#' Logbook highlights
#'
#' Creates a data frame with the highlights of the logbook
#'
#' This function creates a data frame to have an overview of some highlights of the diving logbook
#' such as number of recorded dives, maximum bottom time or maximum depth
#'
#' @param path directory location where the files will be read from or written into
#'
#' @author Ruben Perez Perez
#'
#' @import tidyr
#' @import dplyr
#' @import stringr
#'
#' @return Returns a data frame.
#'
#' @export
#.........................................................
logbook_highlights <- function(path = getwd()){
if(dir.exists(paste0(path, "/divewatchr_data"))){
load('divewatchr_data/scuba_clean.RData')
}
# } else {
#
# load('data/scuba_clean.RData')
# }
highlights <- gather(data.frame("Number_of_dives" = as.character(nrow(scuba_clean)),
"Number_of_dive_sites" = paste0(nrow(distinct(select(scuba_clean, locationID))),
" dive sites (",
nrow(distinct(select(scuba_clean, country))),
" countries)"),
"Maximum_depth" = paste0(max(as.numeric(scuba_clean$maximumDepthInMeters), na.rm = TRUE),
" meters"),
"Total_bottom_time" = paste0(round(sum(as.numeric(scuba_clean$bottomTime), na.rm = TRUE)/60),
" hours (",
sum(!is.na(as.numeric(scuba_clean$bottomTime))),
" bottom times recorded)"),
"Maximum_bottom_time_in_a_dive" = paste0(max(as.numeric(scuba_clean$bottomTime), na.rm = TRUE),
" minutes"),
"Diving_since" = paste0(round(as.numeric(difftime(Sys.Date(), min(scuba_clean$eventDate), units = "days"))/365.25),
" years ago"),
"Last_dive" = paste0(max(scuba_clean$eventDate),
" = ",
paste0(round(as.numeric(difftime(Sys.Date(), max(scuba_clean$eventDate), units = "days"))), " days ago"))
),
key = "Recorded",
value = "Value")
highlights$Recorded <- highlights$Recorded %>% str_replace_all("_", " ")
return(highlights)
}
########################################################################################################################################
# Using pipes
# data.frame("Number_of_dives" = scuba_clean %>% nrow() %>%
# as.character(),
# "Maximum_depth_recorded" = paste0(scuba_clean$maximumDepthInMeters %>% as.numeric() %>%
# max(na.rm = TRUE),
# " meters"),
# "Total_bottom_time" = paste0(scuba_clean$bottomTime %>% as.numeric() %>%
# round(sum(na.rm = TRUE)/60) %>%
# round(),
# " hours"),
# "Maximum_bottom_time_in_a_dive_recorded" = paste0(scuba_clean$bottomTime %>% as.numeric() %>%
# max(na.rm = TRUE),
# " minutes")),
# "Diving_since" = paste0()
#
#
#
# ))