Skip to content

Commit

Permalink
add new filter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MatGreco90 committed Dec 4, 2023
1 parent 5d7fc42 commit 2de0134
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Imports:
rlang,
tidyr,
utils,
lubridate,
vroom
Suggests:
here,
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
130 changes: 130 additions & 0 deletions R/filter.R
Original file line number Diff line number Diff line change
@@ -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)
}








20 changes: 20 additions & 0 deletions man/filter_by_coordinates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/filter_by_month.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/filter_by_species.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/filter_by_year.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/reshape_forcis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2de0134

Please sign in to comment.