Commit ea0c64cb authored by Helmer Belbo's avatar Helmer Belbo
Browse files

Regioninndeling: Changing variable name for year from "år" to "aar" to avod coding trouble.

parent f46f383f
...@@ -16,8 +16,13 @@ ...@@ -16,8 +16,13 @@
#' @export #' @export
#' #'
#' @examples regnavn.at.ref.yr(regionstat = t12750(), ref.yr = 2020 ) %>% glimpse() #' @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 = 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 # Fetch the relevant region reference table regref
...@@ -43,7 +48,8 @@ glimpse(regionstat[seq.int(1, dim(regionstat)[1], length.out = 10), ]) ...@@ -43,7 +48,8 @@ glimpse(regionstat[seq.int(1, dim(regionstat)[1], length.out = 10), ])
regionstat <- regionstat <-
regionstat %>% regionstat %>%
dplyr::mutate(., 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. # when to fit with the ref.yr.
regref_ref.yr_col_code = regref_ref.yr_col_code =
max( which(regref_yr <= ref.yr & regref_typ == "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), ]) ...@@ -58,14 +64,14 @@ glimpse(regionstat[seq.int(1, dim(regionstat)[1], length.out = 10), ])
regionstat %>% regionstat %>%
dplyr::mutate(., dplyr::mutate(.,
# For each obsrv in the regionstat: # 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 # = ar_regref_col_rcode and ar_regref_col_rname
yr_regref_col_rcode = purrr::pmap_int(., .f = function(ar, ...){ yr_regref_col_rcode = purrr::pmap_int(., .f = function(aar, ...){
max( which( regref_yr <= ar & regref_typ == "code") ) max( which( regref_yr <= aar & regref_typ == "code") )
}), }),
yr_regref_col_rname = purrr::pmap_int(., .f = function(ar, ...){ yr_regref_col_rname = purrr::pmap_int(., .f = function(aar, ...){
max( which( regref_yr <= ar & regref_typ == "name") ) max( which( regref_yr <= aar & regref_typ == "name") )
})) }))
print("Samples from regionstat after adding yr_regref_col_rcode:") print("Samples from regionstat after adding yr_regref_col_rcode:")
glimpse(regionstat[seq.int(1, dim(regionstat)[1], length.out = 10), ]) glimpse(regionstat[seq.int(1, dim(regionstat)[1], length.out = 10), ])
...@@ -153,14 +159,14 @@ regnavn.at.ref.yr2 <- function(regionstat){ ...@@ -153,14 +159,14 @@ regnavn.at.ref.yr2 <- function(regionstat){
regionstat %>% regionstat %>%
dplyr::mutate(., dplyr::mutate(.,
# which row in regref the region_code and observation year belongs to: # which row in regref the region_code and observation year belongs to:
regrefrow = purrr::pmap_int(., .f = function(region_kode, 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 <= ar # 1: check if exists a part of regref where "region_code_from" == region_kode AND yfrom <= aar
tmprr <- tmprr <-
regref %>% regref %>%
filter(., reg_code_from == region_kode, yfrom <= ar) filter(., reg_code_from == region_kode, yfrom <= aar)
if (nrow(tmprr) > 0 ) { 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 { } else {
rrr <- NA_integer_ rrr <- NA_integer_
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment