Skip to contents

This function aims at giving the vector of species names corresponding to the observations 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_all_species(
  df,
  spp_col,
  obstype_col = NULL,
  animal_code = "animal",
  return_df = ifelse(is.null(obstype_col), FALSE, TRUE)
)

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 species names.

Value

species names in the same order as df. 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 species and observation type in the same order as df. This dataframe has the following columns (type character):

  • 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). Else, returns only the character vector containing the 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_all_species(df, spp_col = "species", obstype_col = "type")
#>         species   type  species_orig
#> 1        rabbit animal        rabbit
#> 2           cat animal           cat
#> 3           cat animal           cat
#> 4          fire   fire          <NA>
#> 5         blank  blank          <NA>
#> 6 cameratrapper  human cameratrapper
#> 7       tourist  human       tourist
# Use the type column but return a vector
get_all_species(df, spp_col = "species", return_df = FALSE)
#> [1] "rabbit"        "cat"           "cat"           NA             
#> [5] NA              "cameratrapper" "tourist"      

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