Skip to content

Commit

Permalink
Merge pull request #149 from inbo/create_individuals()
Browse files Browse the repository at this point in the history
Create `individuals()`
  • Loading branch information
peterdesmet authored Dec 17, 2024
2 parents 180b088 + 2600a67 commit 1484fc9
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(example_dataset)
export(filter_deployments)
export(filter_media)
export(filter_observations)
export(individuals)
export(locations)
export(media)
export(merge_camtrapdp)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* New function `write_eml()` transforms Camtrap DP metadata to EML (#99).
* New function `round_coordinates()` allows to fuzzy/generalize location information by rounding deployment `latitude` and `longitude`. It also updates `coordinateUncertainty` in the deployments and `coordinatePrecision` and spatial scope in the metadata (#106).
* New function `shift_time()` allows to shift/correct date-times in data and metadata for specified deploymentIDs and duration (#108).
* New function `individuals()` returns a data frame with unique individuals (#149).
* `filter_deployments()` and `deployments()<-` now update the spatial, temporal and taxonomic scope in the metadata based on the returned data (#100, #132).
* `filter_observations()`, `filter_media()`, `media()<-` and `observations()<-` now update the taxonomic scope in the metadata based on the returned data (#89, #100, #130).
* `read_camtrapdp()` now updates the spatial and temporal scope in metadata based on the data (#130).
Expand Down
28 changes: 28 additions & 0 deletions R/individuals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' Get individuals
#'
#' Gets the (unique) individuals from the observations of a Camera Trap Data
#' Package object.
#'
#' @inheritParams print.camtrapdp
#' @return A [tibble::tibble()] data frame with the individuals that have an
#' `individualID`, containing the following columns:
#' - `individualID`
#' - `scientificName`
#' - `lifeStage`
#' - `sex`
#' @family accessor functions
#' @export
#' @examples
#' x <- example_dataset()
#' individuals(x)
individuals <- function(x) {
check_camtrapdp(x)
observations(x) %>%
dplyr::filter(!is.na(individualID)) %>%
dplyr::distinct(
.data$individualID,
.data$scientificName,
.data$lifeStage,
.data$sex
)
}
1 change: 1 addition & 0 deletions man/deployments.Rd

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

1 change: 1 addition & 0 deletions man/events.Rd

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

39 changes: 39 additions & 0 deletions man/individuals.Rd

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

1 change: 1 addition & 0 deletions man/locations.Rd

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

1 change: 1 addition & 0 deletions man/media.Rd

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

1 change: 1 addition & 0 deletions man/observations.Rd

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

1 change: 1 addition & 0 deletions man/taxa.Rd

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

32 changes: 32 additions & 0 deletions tests/testthat/test-individuals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
test_that("individuals() returns a tibble", {
skip_if_offline()
x <- example_dataset()
expect_s3_class(individuals(x), "tbl")
})

test_that("individuals() returns the expected columns", {
skip_if_offline()
x <- example_dataset()
expected_cols <- c(
"individualID",
"scientificName",
"lifeStage",
"sex"
)
expect_named(individuals(x), expected_cols)
})

test_that("individuals() returns the expected rows (unique individuals)", {
skip_if_offline()
x <- example_dataset()

# Expect 0 individuals
expect_equal(nrow(individuals(x)), 0)

# Add 3 individuals to event 79204343
# TODO: remove once https://github.com/tdwg/camtrap-dp/issues/396 is released
x$data$observations[x$data$observations$observationID == "05230014",]$individualID <- "Fezzik"
x$data$observations[x$data$observations$observationID == "5ead5692",]$individualID <- "Buttercup"
x$data$observations[x$data$observations$observationID == "29939500",]$individualID <- "Westley"
expect_equal(nrow(individuals(x)), 3)
})

0 comments on commit 1484fc9

Please sign in to comment.