Extending results at product level with columns from input datasets
Source:vignettes/articles/extending-outputs.Rmd
extending-outputs.Rmd
This article shows how to extend results at product level with columns from input datasets.
library(tiltIndicator)
library(tiltToyData)
library(tibble, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
library(readr, warn.conflicts = FALSE)
options(readr.show_col_types = FALSE)
Any column in the input datasets ending with *rowid
is
also passed as is to the output at product level. The exception is any
column named exactly rowid
– which is a reserved name and
throws an error. Note this feature makes no sense at company level
because potentially multiple rows in the input datasets are summarized
into a single row in the output at company level.
This example shows emissions_profile()
but the same goes
for other functions.
companies <- read_csv(toy_emissions_profile_any_companies())
products <- read_csv(toy_emissions_profile_products_ecoinvent()) |>
rowid_to_column("products_rowid")
result_at_product_level <- emissions_profile(companies, products) |>
unnest_product()
result_at_product_level
#> # A tibble: 456 × 8
#> companies_id products_rowid grouped_by risk_category profile_ranking
#> <chr> <int> <chr> <chr> <dbl>
#> 1 antimonarchy_canine 3 all high 1
#> 2 antimonarchy_canine 3 isic_4digit high 1
#> 3 antimonarchy_canine 3 tilt_subsec… high 1
#> 4 antimonarchy_canine 3 unit high 1
#> 5 antimonarchy_canine 3 unit_isic_4… high 1
#> 6 antimonarchy_canine 3 unit_tilt_s… high 1
#> 7 celestial_lovebird 3 all high 1
#> 8 celestial_lovebird 3 isic_4digit high 1
#> 9 celestial_lovebird 3 tilt_subsec… high 1
#> 10 celestial_lovebird 3 unit high 1
#> # ℹ 446 more rows
#> # ℹ 3 more variables: clustered <chr>, activity_uuid_product_uuid <chr>,
#> # co2_footprint <dbl>
The *rowid
column allows you to extend the output at
product level with columns from the input.
extension <- products |> select(matches(c("rowid", "sector", "isic")))
extension
#> # A tibble: 3 × 4
#> products_rowid tilt_sector tilt_subsector isic_4digit
#> <int> <chr> <chr> <chr>
#> 1 1 metals iron & steel '2410'
#> 2 2 metals other metals '2591'
#> 3 3 construction construction residential '4100'
extended <- result_at_product_level |> left_join(extension)
#> Joining with `by = join_by(products_rowid)`
extended
#> # A tibble: 456 × 11
#> companies_id products_rowid grouped_by risk_category profile_ranking
#> <chr> <int> <chr> <chr> <dbl>
#> 1 antimonarchy_canine 3 all high 1
#> 2 antimonarchy_canine 3 isic_4digit high 1
#> 3 antimonarchy_canine 3 tilt_subsec… high 1
#> 4 antimonarchy_canine 3 unit high 1
#> 5 antimonarchy_canine 3 unit_isic_4… high 1
#> 6 antimonarchy_canine 3 unit_tilt_s… high 1
#> 7 celestial_lovebird 3 all high 1
#> 8 celestial_lovebird 3 isic_4digit high 1
#> 9 celestial_lovebird 3 tilt_subsec… high 1
#> 10 celestial_lovebird 3 unit high 1
#> # ℹ 446 more rows
#> # ℹ 6 more variables: clustered <chr>, activity_uuid_product_uuid <chr>,
#> # co2_footprint <dbl>, tilt_sector <chr>, tilt_subsector <chr>,
#> # isic_4digit <chr>