R/pick_drop_var_cond.R
pick_drop.Rd
These functions provide an expressive and convenient way to pick specific
rows from ForestGEO datasets. They allow you to remove missing values (with
na.rm = TRUE
) but conservatively default to preserving them. This behavior
is similar to base::subset()
and unlike dplyr::filter()
. This
conservative default is important because you want want to include missing
trees in your analysis.
pick_dbh_min(data, value, na.rm = FALSE)
pick_dbh_max(data, value, na.rm = FALSE)
pick_dbh_under(data, value, na.rm = FALSE)
pick_dbh_over(data, value, na.rm = FALSE)
pick_status(data, value, na.rm = FALSE)
drop_status(data, value, na.rm = FALSE)
A ForestGEO-like dataframe: A ViewFullTable, tree or stem table.
An atomic vector; a single value against to compare each value of the variable encoded in the function's name.
Set to TRUE
if you want to remove missing values from the
variable encoded in the function's name.
A dataframe similar to .data
but including only the rows with
matching conditions.
dplyr::filter()
, Extract
([
).
Other functions for fgeo census and vft:
guess_plotdim()
Other functions for fgeo census:
add_status_tree()
,
add_var()
,
guess_plotdim()
Other functions for fgeo vft:
add_status_tree()
,
add_subquad()
,
add_var()
,
guess_plotdim()
Other functions to pick or drop rows of a ForestGEO dataframe:
pick_main_stem()
# styler: off
census <- tribble(
~dbh, ~status,
0, "A",
50, "A",
100, "A",
150, "A",
NA, "M",
NA, "D",
NA, NA
)
# styler: on
# <=
pick_dbh_max(census, 100)
#> # A tibble: 6 × 2
#> dbh status
#> <dbl> <chr>
#> 1 0 A
#> 2 50 A
#> 3 100 A
#> 4 NA M
#> 5 NA D
#> 6 NA NA
pick_dbh_max(census, 100, na.rm = TRUE)
#> # A tibble: 3 × 2
#> dbh status
#> <dbl> <chr>
#> 1 0 A
#> 2 50 A
#> 3 100 A
# >=
pick_dbh_min(census, 100)
#> # A tibble: 5 × 2
#> dbh status
#> <dbl> <chr>
#> 1 100 A
#> 2 150 A
#> 3 NA M
#> 4 NA D
#> 5 NA NA
pick_dbh_min(census, 100, na.rm = TRUE)
#> # A tibble: 2 × 2
#> dbh status
#> <dbl> <chr>
#> 1 100 A
#> 2 150 A
# <
pick_dbh_under(census, 100)
#> # A tibble: 5 × 2
#> dbh status
#> <dbl> <chr>
#> 1 0 A
#> 2 50 A
#> 3 NA M
#> 4 NA D
#> 5 NA NA
pick_dbh_under(census, 100, na.rm = TRUE)
#> # A tibble: 2 × 2
#> dbh status
#> <dbl> <chr>
#> 1 0 A
#> 2 50 A
# >
pick_dbh_over(census, 100)
#> # A tibble: 4 × 2
#> dbh status
#> <dbl> <chr>
#> 1 150 A
#> 2 NA M
#> 3 NA D
#> 4 NA NA
pick_dbh_over(census, 100, na.rm = TRUE)
#> # A tibble: 1 × 2
#> dbh status
#> <dbl> <chr>
#> 1 150 A
# Same, but `subset()` does not let you keep NAs.
subset(census, dbh > 100)
#> # A tibble: 1 × 2
#> dbh status
#> <dbl> <chr>
#> 1 150 A
# ==
pick_status(census, "A")
#> # A tibble: 5 × 2
#> dbh status
#> <dbl> <chr>
#> 1 0 A
#> 2 50 A
#> 3 100 A
#> 4 150 A
#> 5 NA NA
pick_status(census, "A", na.rm = TRUE)
#> # A tibble: 4 × 2
#> dbh status
#> <dbl> <chr>
#> 1 0 A
#> 2 50 A
#> 3 100 A
#> 4 150 A
# !=
drop_status(census, "D")
#> # A tibble: 6 × 2
#> dbh status
#> <dbl> <chr>
#> 1 0 A
#> 2 50 A
#> 3 100 A
#> 4 150 A
#> 5 NA M
#> 6 NA NA
drop_status(census, "D", na.rm = TRUE)
#> # A tibble: 5 × 2
#> dbh status
#> <dbl> <chr>
#> 1 0 A
#> 2 50 A
#> 3 100 A
#> 4 150 A
#> 5 NA M
# Compose
pick_dbh_over(
drop_status(census, "D", na.rm = TRUE),
100
)
#> # A tibble: 2 × 2
#> dbh status
#> <dbl> <chr>
#> 1 150 A
#> 2 NA M
# More readable as a pipiline
census %>%
drop_status("D", na.rm = TRUE) %>%
pick_dbh_over(100)
#> # A tibble: 2 × 2
#> dbh status
#> <dbl> <chr>
#> 1 150 A
#> 2 NA M
# Also works with ViewFullTables
# styler: off
vft <- tribble(
~DBH, ~Status,
0, "alive",
50, "alive",
100, "alive",
150, "alive",
NA, "missing",
NA, "dead",
NA, NA
)
# styler: on
pick_dbh_max(vft, 100)
#> # A tibble: 6 × 2
#> DBH Status
#> <dbl> <chr>
#> 1 0 alive
#> 2 50 alive
#> 3 100 alive
#> 4 NA missing
#> 5 NA dead
#> 6 NA NA
pick_status(vft, "alive", na.rm = TRUE)
#> # A tibble: 4 × 2
#> DBH Status
#> <dbl> <chr>
#> 1 0 alive
#> 2 50 alive
#> 3 100 alive
#> 4 150 alive