diff --git a/R/no_regioninndeling.R b/R/no_regioninndeling.R index 24d9813577717f58a817a44dda4dcd50ec5c8be9..21a60ab5560f4d7e74b1b5ef4c48a52ca3516167 100644 --- a/R/no_regioninndeling.R +++ b/R/no_regioninndeling.R @@ -16,8 +16,13 @@ #' @export #' #' @examples regnavn.at.ref.yr(regionstat = t12750(), ref.yr = 2020 ) %>% glimpse() -regnavn.at.ref.yr <- function(regionstat, ref.yr = year(now()), reg_level = "fylke"){ +regnavn.at.ref.yr <- function(regionstat, ref.yr = lubridate::year(now()), reg_level = "fylke"){ # regionstat = t12750() #for testing + regionstat <- as_tibble(regionstat) + if (!"aar" %in% names(regionstat)) { + w <- which(tolower(names(regionstat)) %in% c("ar", "år") ) + regionstat$aar = as.numeric(pull(regionstat[,w])) + } # Fetch the relevant region reference table regref @@ -43,7 +48,8 @@ glimpse(regionstat[seq.int(1, dim(regionstat)[1], length.out = 10), ]) regionstat <- regionstat %>% dplyr::mutate(., - # for each obs in regionstat: tag which column in regreft one should fetch the name and code + # for each obs in regionstat: tag which column in + # regref one should fetch the name and code # when to fit with the ref.yr. regref_ref.yr_col_code = max( which(regref_yr <= ref.yr & regref_typ == "code") ), @@ -58,14 +64,14 @@ glimpse(regionstat[seq.int(1, dim(regionstat)[1], length.out = 10), ]) regionstat %>% dplyr::mutate(., # For each obsrv in the regionstat: - # which column in regref the "ar" belongs to; + # which column in regref the "aar" belongs to; # = ar_regref_col_rcode and ar_regref_col_rname - yr_regref_col_rcode = purrr::pmap_int(., .f = function(ar, ...){ - max( which( regref_yr <= ar & regref_typ == "code") ) + yr_regref_col_rcode = purrr::pmap_int(., .f = function(aar, ...){ + max( which( regref_yr <= aar & regref_typ == "code") ) }), - yr_regref_col_rname = purrr::pmap_int(., .f = function(ar, ...){ - max( which( regref_yr <= ar & regref_typ == "name") ) + yr_regref_col_rname = purrr::pmap_int(., .f = function(aar, ...){ + max( which( regref_yr <= aar & regref_typ == "name") ) })) print("Samples from regionstat after adding yr_regref_col_rcode:") glimpse(regionstat[seq.int(1, dim(regionstat)[1], length.out = 10), ]) @@ -153,14 +159,14 @@ regnavn.at.ref.yr2 <- function(regionstat){ regionstat %>% dplyr::mutate(., # which row in regref the region_code and observation year belongs to: - regrefrow = purrr::pmap_int(., .f = function(region_kode, ar, ...){ - # 1: check if exists a part of regref where "region_code_from" == region_kode AND yfrom <= ar + regrefrow = purrr::pmap_int(., .f = function(region_kode, aar, ...){ + # 1: check if exists a part of regref where "region_code_from" == region_kode AND yfrom <= aar tmprr <- regref %>% - filter(., reg_code_from == region_kode, yfrom <= ar) + filter(., reg_code_from == region_kode, yfrom <= aar) if (nrow(tmprr) > 0 ) { - rrr <- max( which(regref$reg_code_from == region_kode & regref$yfrom <= ar)) + rrr <- max( which(regref$reg_code_from == region_kode & regref$yfrom <= aar)) } else { rrr <- NA_integer_ }