diff --git a/DESCRIPTION b/DESCRIPTION index 00144d1..5e3fd5b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -40,6 +40,7 @@ Imports: rlang, tidyr, utils, + lubridate, vroom Suggests: here, diff --git a/NAMESPACE b/NAMESPACE index 4a19d61..67b2403 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,10 @@ export(compute_abundances) export(compute_concentration_bins_CPR_n) export(compute_concentrations) export(compute_frequencies) +export(filter_by_coordinates) +export(filter_by_month) +export(filter_by_species) +export(filter_by_year) export(get_cpr_north_data) export(get_cpr_south_data) export(get_forcis_db) @@ -12,9 +16,11 @@ export(get_pump_data) export(get_required_columns) export(get_sediment_trap_data) export(get_species_names) +export(reshape_forcis) export(select_columns) export(select_taxonomy) import(dplyr) +import(lubridate) import(rlang) import(tidyr) import(vroom) diff --git a/R/filter.R b/R/filter.R new file mode 100644 index 0000000..790fcd4 --- /dev/null +++ b/R/filter.R @@ -0,0 +1,130 @@ + +#' Reshape and simplify forcis data +#' +#' @param data forcis data +#' +#' @return A `data.frame` +#' @export +#' +#' +reshape_forcis <- function(data){ + + if (get_data_type(data) %in% c("CPR North")) { + stop(paste0("This function is not designed to work with 'CPR North' data"), call. = FALSE) + } + + taxa_cols <- get_species_names(data) + metadat_cols <- get_required_columns() + dat_reshaped <- dat_reshaped %>% + select(all_of(taxa_cols),metadat_cols) %>% + pivot_longer(all_of(taxa_cols), + names_to = 'taxa', + values_to = 'counts') + return(dat_reshaped) +} + + + +#' Filter forcis data by year of sampling +#' +#' @param data forcis data in long format +#' @param years numeric vector of selected years +#' +#' @return A `data.frame`. +#' @export +#' +#' @import lubridate +#' +filter_by_year <- function(data,years){ + year_vector <- as.numeric(years) + filtered_dat <- data %>% + filter(! is.na(.data$profile_date_time)) %>% + mutate(new_profile_date_time = dmy(.data$profile_date_time)) %>% + mutate(year=year(.data$new_profile_date_time)) %>% + filter(.data$year %in% year_vector) %>% + select(-c(.data$year,.data$new_profile_date_time)) + + return(filtered_dat) + +} + + +#' Filter forcis data by month of sampling +#' +#' @param data forcis data in long format +#' @param months numeric vector of selected months +#' +#' @return A `data.frame`. +#' @export +#' +#' @import lubridate +filter_by_month <- function(data,months){ + + month_vector <- as.numeric(months) + + filtered_dat <- data %>% + filter(! is.na(.data$profile_date_time)) %>% + mutate(new_profile_date_time =dmy(.data$profile_date_time)) %>% + mutate(month=month(.data$new_profile_date_time)) %>% + filter(.data$month %in% month_vector)%>% + select(-c(.data$month,.data$new_profile_date_time)) + + return(filtered_dat) + +} + + +#' Filter forcis data by coordinate square +#' +#' @param data forcis data in long format +#' @param coord_square a numeric vector containing in this order minimum latitute, +#' minimum longitude, maximum latitude, maximum longitude +#' +#' @return A `data.frame`. +#' @export +#' +#' @examples +filter_by_coordinates <- function(data, coord_square){ + + min_lat <- coord_square[1] + min_long <- coord_square[2] + max_lat <- coord_square[3] + max_long <- coord_square[3] + + filtered_dat <- data %>% + filter(! is.na(.data$site_lat_start_decimal)) %>% + filter(! is.na (.data$site_lon_start_decimal)) %>% + filter(.data$site_lat_start_decimal>= min_lat & + .data$site_lat_start_decimal <=max_lat & + .data$site_lon_start_decimal>= min_long & + .data$site_lon_start_decimal <=max_long) + + return(filtered_dat) +} + +#' Filter forcis data by species +#' +#' @param data forcis data in long format, except for CPR North data +#' @param species a character vector listing species of interest +#' +#' @return A `data.frame` +#' @export +#' +#' @examples +filter_by_species <- function (data,species ){ + my_species <- as.character(species) + + filtered_dat <- data %>% + filter(.data$taxa %in% my_species) + # filter(! is.na(counts)) + + return(filtered_dat) +} + + + + + + + + diff --git a/man/filter_by_coordinates.Rd b/man/filter_by_coordinates.Rd new file mode 100644 index 0000000..3c9e4d6 --- /dev/null +++ b/man/filter_by_coordinates.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/filter.R +\name{filter_by_coordinates} +\alias{filter_by_coordinates} +\title{Filter forcis data by coordinate square} +\usage{ +filter_by_coordinates(data, coord_square) +} +\arguments{ +\item{data}{forcis data in long format} + +\item{coord_square}{a numeric vector containing in this order minimum latitute, +minimum longitude, maximum latitude, maximum longitude} +} +\value{ +A \code{data.frame}. +} +\description{ +Filter forcis data by coordinate square +} diff --git a/man/filter_by_month.Rd b/man/filter_by_month.Rd new file mode 100644 index 0000000..ca179bf --- /dev/null +++ b/man/filter_by_month.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/filter.R +\name{filter_by_month} +\alias{filter_by_month} +\title{Filter forcis data by month of sampling} +\usage{ +filter_by_month(data, months) +} +\arguments{ +\item{data}{forcis data in long format} + +\item{months}{numeric vector of selected months} +} +\value{ +A \code{data.frame}. +} +\description{ +Filter forcis data by month of sampling +} diff --git a/man/filter_by_species.Rd b/man/filter_by_species.Rd new file mode 100644 index 0000000..9f4b965 --- /dev/null +++ b/man/filter_by_species.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/filter.R +\name{filter_by_species} +\alias{filter_by_species} +\title{Filter forcis data by species} +\usage{ +filter_by_species(data, species) +} +\arguments{ +\item{data}{forcis data in long format, except for CPR North data} + +\item{species}{a character vector listing species of interest} +} +\value{ +A \code{data.frame} +} +\description{ +Filter forcis data by species +} diff --git a/man/filter_by_year.Rd b/man/filter_by_year.Rd new file mode 100644 index 0000000..dfb4c3b --- /dev/null +++ b/man/filter_by_year.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/filter.R +\name{filter_by_year} +\alias{filter_by_year} +\title{Filter forcis data by year of sampling} +\usage{ +filter_by_year(data, years) +} +\arguments{ +\item{data}{forcis data in long format} + +\item{years}{numeric vector of selected years} +} +\value{ +A \code{data.frame}. +} +\description{ +Filter forcis data by year of sampling +} diff --git a/man/reshape_forcis.Rd b/man/reshape_forcis.Rd new file mode 100644 index 0000000..43e9c48 --- /dev/null +++ b/man/reshape_forcis.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/filter.R +\name{reshape_forcis} +\alias{reshape_forcis} +\title{Reshape and simplify forcis data} +\usage{ +reshape_forcis(data) +} +\arguments{ +\item{data}{forcis data} +} +\value{ +A \code{data.frame} +} +\description{ +Reshape and simplify forcis data +}