Get a vector with all unique values found in a given column of a data.frame.
Concatenated values (A,B
) in the column can be returned as single values
(A
and B
).
Arguments
- .data
Data frame. Data.frame to select column from.
- column
Character or integer. Quoted or unqoted column name or column position.
- split
Character (vector). Character or regular expression(s) passed to
strsplit()
to split column values before returning unique values. Defaults to,
.
Examples
# Set default connection variable
con <- connect_to_etn()
library(dplyr) # For %>%
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
# List unique scientific_name from a dataframe containing animal information
df <- get_animals(con, animal_project_code = "2014_demer")
list_values(df, "scientific_name")
#> 4 unique scientific_name values
#> [1] "Rutilus rutilus" "Silurus glanis" "Squalius cephalus"
#> [4] "Petromyzon marinus"
# Or using pipe and unquoted column name
df %>% list_values(scientific_name)
#> 4 unique scientific_name values
#> [1] "Rutilus rutilus" "Silurus glanis" "Squalius cephalus"
#> [4] "Petromyzon marinus"
# Or using column position
df %>% list_values(8)
#> 1 unique scientific_name values
#> # A tibble: 4 × 1
#> scientific_name
#> <chr>
#> 1 Rutilus rutilus
#> 2 Silurus glanis
#> 3 Squalius cephalus
#> 4 Petromyzon marinus
# tag_serial_number can contain comma-separated values
df <- get_animals(con, animal_id = 5841)
df$tag_serial_number
#> [1] "1280688,1280688"
# list_values() will split those and return unique values
list_values(df, tag_serial_number)
#> 1 unique tag_serial_number values
#> [1] "1280688"
# Another expression can be defined to split values (here ".")
list_values(df, tag_serial_number, split = "\\.")
#> 1 unique tag_serial_number values
#> [1] "1280688,1280688"