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).
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
isTRUE
: a columnID
to uniquely identify each species/observation combination (IDs are numbers).type
is the observation type value. Else, IDs are of the formspp
.a column named like
spp_col
containing species names (whereNA
values inspp_col
are replaced as described above).if
obstype_col
is provided: a column named likeobstype_col
containing corresponding observations types.if
obstype_col
is provided: a column named likespp_col
with a suffix_orig
which indicates the original value ofspp_col
(before it was maybe replaced withNA
). Ifreturn_df
isFALSE
, returns only the unique values ofspp_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