@@ -2594,9 +2594,8 @@ calculate_lagged <- function(
25942594# ' Calculate EDGAR covariates
25952595# ' @description
25962596# ' Extract EDGAR yearly sector VOC values at point locations. Returns a
2597- # ' \code{data.frame} object containing \code{locs_id} and EDGAR voc variables .
2597+ # ' \code{data.frame} object containing \code{locs_id} and EDGAR voc variable .
25982598# ' variable column names reflect the VOC number and circular buffer radius.
2599- # ' sector column reflects the sector generating the VOC emissions.
26002599# ' @param from SpatRaster(1). Output from \code{process_edgar()}.
26012600# ' @param locs data.frame. character to file path, SpatVector, or sf object.
26022601# ' @param locs_id character(1). Column within `locations` CSV file
@@ -2618,7 +2617,13 @@ calculate_lagged <- function(
26182617# ' @importFrom terra extract
26192618# ' @importFrom terra nlyr
26202619# ' @importFrom terra crs
2620+ # ' @importFrom dplyr matches
2621+ # ' @importFrom dplyr rename
2622+ # ' @importFrom tidyr pivot_wider
2623+ # ' @importFrom stringr str_extract
26212624# ' @examples
2625+ # ' ## NOTE: Current function only supports one VOC number in 'from' SpatRaster.
2626+ # ' ## User should process and calculate each VOC number separately.
26222627# ' ## NOTE: Example is wrapped in `\dontrun{}` as function requires a large
26232628# ' ## amount of data which is not included in the package.
26242629# ' \dontrun{
@@ -2666,14 +2671,27 @@ calculate_edgar <- function(
26662671 time_type = " year" ,
26672672 ...
26682673 )
2674+
2675+ # ### pivot to wide dataframe by sector
2676+ sites_transformed <- sites_extracted %> %
2677+ # Rename VOC column to a generic name for easier manipulation
2678+ dplyr :: rename(value = dplyr :: matches(" ^voc\\ d+_\\ d+$" )) %> %
2679+ # Extract species and radius from the VOC column name
2680+ mutate(
2681+ species = str_extract(names(sites_extracted )[4 ], " ^voc\\ d+" ),
2682+ radius = str_extract(names(sites_extracted )[4 ], " \\ d+$" ),
2683+ variable = paste0(species , " _" , level , " _" , radius )
2684+ ) %> %
2685+ # Drop unneeded columns and pivot
2686+ select(- level , - species , - radius ) %> %
2687+ pivot_wider(names_from = variable , values_from = value )
2688+
26692689 sites_return <- amadeus :: calc_return_locs(
2670- covar = sites_extracted ,
2690+ covar = sites_transformed ,
26712691 POSIXt = FALSE ,
26722692 geom = geom ,
26732693 crs = terra :: crs(from )
26742694 )
2675- # ### change level column to "sector" name
2676- names(sites_return )[3 ] <- " sector"
26772695
26782696 # ### return data.frame
26792697 return (sites_return )
0 commit comments