Takes a data frame with a column of type codes (main type or subtype codes), and, under certain conditions, adds new rows with codes of the associated subtypes and main types, respectively. It allows to do sensible selections and joins with interpreted forms of the habitatmap_stdized and watersurfaces_hab data sources: habitatmap_terr, read_watersurfaces_hab(interpreted = TRUE). If the data frame has one or more grouping variables, by default the operation is done independently for each group in turn.

expand_types(x, type_var = "type", use_grouping = TRUE, strict = TRUE)

Arguments

x

An object of class data.frame.

type_var

A string. The name of the data frame variable that holds the type codes. Defaults to type.

use_grouping

Logical. If the data frame has one or more grouping variables (class grouped_df), is the operation to be performed independently for each group in turn?

strict

Logical. Apply conditions before expanding subtype codes to main type codes?

Value

A data frame, either identical or longer than the input data frame.

Details

The extra rows in the data frame take the values for other variables from the rows with which they are associated, based on the subtype - main type relation. Type codes in the data frame are verified to comply with the codes from the types data source. A warning is given when they don't.

Main type codes are always expanded with the subtype codes that belong to it.

The applied approach to add main type codes only makes sense assuming that the result is to be confronted with one of the above listed geospatial data sources.

In order to add main type codes based on subtype codes that are present in the type column, specific conditions have to be met:

  • for 2330: both subtype codes must be present

  • for 5130: 5130_hei must be present (note that only the main type code occurs in the targeted data sources)

  • for 6230: 6230_ha, 6230_hmo and 6230_hn must be present (not the rare 6230_hnk)

  • for 91E0: 91E0_va, 91E0_vm and 91E0_vn must be present (not the rarer 91E0_sf, 91E0_vc and 91E0_vo)

However, it is possible to relax this requirement by setting strict = FALSE. This will add the main type code whenever one of the above required subtype codes is present. In all cases no other main type codes are added apart from 2330, 5130, 6230 and 91E0. This is because the data sources with which the result is to be matched (see Description) don't contain certain main type codes, and because it makes no sense in other cases (rbbkam, rbbzil & 9120 in the habitatmap do not refer to a main type but to a non-defined subtype with no specific code).

See also

Examples

library(dplyr)
#> 
#> 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
x <-
  n2khabmon::read_scheme_types() %>%
  filter(scheme == "GW_05.1_terr")
expand_types(x)
#> # A tibble: 61 × 5
#>    scheme       type     typegroup           typegroup_name  typegroup_shortname
#>    <fct>        <fct>    <fct>               <fct>           <fct>              
#>  1 GW_05.1_terr 1310_pol GW_05.1_terr_group3 types from mod… moderately wet     
#>  2 GW_05.1_terr 1310_zk  GW_05.1_terr_group1 types from ver… very wet           
#>  3 GW_05.1_terr 1310_zv  GW_05.1_terr_group4 types from moi… moist              
#>  4 GW_05.1_terr 1320     GW_05.1_terr_group1 types from ver… very wet           
#>  5 GW_05.1_terr 1330_da  GW_05.1_terr_group1 types from ver… very wet           
#>  6 GW_05.1_terr 1330_hpr GW_05.1_terr_group3 types from mod… moderately wet     
#>  7 GW_05.1_terr 2130_had GW_05.1_terr_group5 types from loc… locally moist      
#>  8 GW_05.1_terr 2130_hd  GW_05.1_terr_group5 types from loc… locally moist      
#>  9 GW_05.1_terr 2160     GW_05.1_terr_group4 types from moi… moist              
#> 10 GW_05.1_terr 2170     GW_05.1_terr_group4 types from moi… moist              
#> # ℹ 51 more rows
expand_types(x, strict = FALSE)
#> # A tibble: 62 × 5
#>    scheme       type     typegroup           typegroup_name  typegroup_shortname
#>    <fct>        <fct>    <fct>               <fct>           <fct>              
#>  1 GW_05.1_terr 1310_pol GW_05.1_terr_group3 types from mod… moderately wet     
#>  2 GW_05.1_terr 1310_zk  GW_05.1_terr_group1 types from ver… very wet           
#>  3 GW_05.1_terr 1310_zv  GW_05.1_terr_group4 types from moi… moist              
#>  4 GW_05.1_terr 1320     GW_05.1_terr_group1 types from ver… very wet           
#>  5 GW_05.1_terr 1330_da  GW_05.1_terr_group1 types from ver… very wet           
#>  6 GW_05.1_terr 1330_hpr GW_05.1_terr_group3 types from mod… moderately wet     
#>  7 GW_05.1_terr 2130_had GW_05.1_terr_group5 types from loc… locally moist      
#>  8 GW_05.1_terr 2130_hd  GW_05.1_terr_group5 types from loc… locally moist      
#>  9 GW_05.1_terr 2160     GW_05.1_terr_group4 types from moi… moist              
#> 10 GW_05.1_terr 2170     GW_05.1_terr_group4 types from moi… moist              
#> # ℹ 52 more rows

x <-
  n2khabmon::read_scheme_types() %>%
  filter(scheme == "GW_05.1_terr") %>%
  group_by(typegroup)
expand_types(x)
#> # A tibble: 58 × 5
#> # Groups:   typegroup [5]
#>    scheme       type     typegroup           typegroup_name  typegroup_shortname
#>    <fct>        <fct>    <fct>               <fct>           <fct>              
#>  1 GW_05.1_terr 1310_pol GW_05.1_terr_group3 types from mod… moderately wet     
#>  2 GW_05.1_terr 1330_hpr GW_05.1_terr_group3 types from mod… moderately wet     
#>  3 GW_05.1_terr 4010     GW_05.1_terr_group3 types from mod… moderately wet     
#>  4 GW_05.1_terr 6410_mo  GW_05.1_terr_group3 types from mod… moderately wet     
#>  5 GW_05.1_terr 6410_ve  GW_05.1_terr_group3 types from mod… moderately wet     
#>  6 GW_05.1_terr 6430_hf  GW_05.1_terr_group3 types from mod… moderately wet     
#>  7 GW_05.1_terr 6430_hw  GW_05.1_terr_group3 types from mod… moderately wet     
#>  8 GW_05.1_terr 6430_mr  GW_05.1_terr_group3 types from mod… moderately wet     
#>  9 GW_05.1_terr rbbhc    GW_05.1_terr_group3 types from mod… moderately wet     
#> 10 GW_05.1_terr rbbhf    GW_05.1_terr_group3 types from mod… moderately wet     
#> # ℹ 48 more rows
expand_types(x, use_grouping = FALSE) # equals above example
#> # A tibble: 61 × 5
#> # Groups:   typegroup [5]
#>    scheme       type     typegroup           typegroup_name  typegroup_shortname
#>    <fct>        <fct>    <fct>               <fct>           <fct>              
#>  1 GW_05.1_terr 1310_pol GW_05.1_terr_group3 types from mod… moderately wet     
#>  2 GW_05.1_terr 1310_zk  GW_05.1_terr_group1 types from ver… very wet           
#>  3 GW_05.1_terr 1310_zv  GW_05.1_terr_group4 types from moi… moist              
#>  4 GW_05.1_terr 1320     GW_05.1_terr_group1 types from ver… very wet           
#>  5 GW_05.1_terr 1330_da  GW_05.1_terr_group1 types from ver… very wet           
#>  6 GW_05.1_terr 1330_hpr GW_05.1_terr_group3 types from mod… moderately wet     
#>  7 GW_05.1_terr 2130_had GW_05.1_terr_group5 types from loc… locally moist      
#>  8 GW_05.1_terr 2130_hd  GW_05.1_terr_group5 types from loc… locally moist      
#>  9 GW_05.1_terr 2160     GW_05.1_terr_group4 types from moi… moist              
#> 10 GW_05.1_terr 2170     GW_05.1_terr_group4 types from moi… moist              
#> # ℹ 51 more rows

x <-
  tribble(
    ~mycode, ~obs,
    "2130", 5,
    "2190", 45,
    "2330_bu", 8,
    "2330_dw", 8,
    "5130_hei", 7,
    "6410_mo", 78,
    "6410_ve", 4,
    "91E0_vn", 10
  )
expand_types(x, type_var = "mycode")
#> # A tibble: 15 × 2
#>    mycode        obs
#>    <chr>       <dbl>
#>  1 2130            5
#>  2 2190           45
#>  3 2330_bu         8
#>  4 2330_dw         8
#>  5 5130_hei        7
#>  6 6410_mo        78
#>  7 6410_ve         4
#>  8 91E0_vn        10
#>  9 2130_had        5
#> 10 2130_hd         5
#> 11 2190_a         45
#> 12 2190_mp        45
#> 13 2190_overig    45
#> 14 2330            8
#> 15 5130            7
expand_types(x, type_var = "mycode", strict = FALSE)
#> # A tibble: 16 × 2
#>    mycode        obs
#>    <chr>       <dbl>
#>  1 2130            5
#>  2 2190           45
#>  3 2330_bu         8
#>  4 2330_dw         8
#>  5 5130_hei        7
#>  6 6410_mo        78
#>  7 6410_ve         4
#>  8 91E0_vn        10
#>  9 2130_had        5
#> 10 2130_hd         5
#> 11 2190_a         45
#> 12 2190_mp        45
#> 13 2190_overig    45
#> 14 2330            8
#> 15 5130            7
#> 16 91E0           10