Skip to contents

This function aims at giving the unique species names corresponding from a dataframe. It is primarily intended for dataframes where observations have a type, and some non-animal species are written as NA but a more general type is provided (as with the camtrapDP standard).

Usage

get_unique_species(
  df,
  spp_col,
  obstype_col = NULL,
  animal_code = "animal",
  return_df = ifelse(is.null(obstype_col), FALSE, TRUE),
  reorder = FALSE,
  add_ID = FALSE
)

Arguments

df

The dataframe

spp_col

name of the species column from the dataframe

obstype_col

name of the observation type column from the dataframe

animal_code

value of obstype_col coding for animal observations.

return_df

return a dataframe? If TRUE, will return a dataframe (see below); else will return a character vector of unique species names.

reorder

Reorder the results? This will arrange values by alphabetical order. If obstype_col is provided, non-animal species will be arranged last.

add_ID

Add an ID column?

Value

unique species names. If obstype_col is provided, NA values in spp_col are replaced with the corresponding value in obstype_col

(if obstype_col is not animal_code). If return_df is TRUE, returns a dataframe containing unique species and observation type. This dataframe has the following columns (type character):

  • If add_ID is TRUE: a column ID to uniquely identify each species/observation combination (IDs are numbers). type is the observation type value. Else, IDs are of the form spp.

  • a column named like spp_col containing species names (where NA values in spp_col are replaced as described above).

  • if obstype_col is provided: a column named like obstype_col containing corresponding observations types.

  • if obstype_col is provided: a column named like spp_col with a suffix _orig which indicates the original value of spp_col (before it was maybe replaced with NA). If return_df is FALSE, returns only the unique values of spp_col.

Examples

df <- data.frame(species = c("rabbit", "cat", "cat", NA, NA, 
                             "cameratrapper", "tourist"),
                 type = c("animal", "animal", "animal", "fire", "blank", 
                          "human", "human"))
# Use the type column
get_unique_species(df, spp_col = "species", obstype_col = "type",
                   reorder = TRUE)
#>         species   type  species_orig
#> 1           cat animal           cat
#> 2        rabbit animal        rabbit
#> 3         blank  blank          <NA>
#> 4          fire   fire          <NA>
#> 5 cameratrapper  human cameratrapper
#> 6       tourist  human       tourist
# Use the type column but return a vector
get_unique_species(df, spp_col = "species", return_df = FALSE,
                   reorder = TRUE)
#> [1] "cameratrapper" "cat"           "rabbit"        "tourist"      
#> [5] NA             

# Don't use the type column
get_unique_species(df, spp_col = "species",
                   reorder = TRUE)
#> [1] "cameratrapper" "cat"           "rabbit"        "tourist"      
#> [5] NA