From ea0c64cb88a00b091ee5121ff3e876532b35922d Mon Sep 17 00:00:00 2001 From: Helmer Belbo <Helmer.Belbo@nibio.no> Date: Mon, 3 Aug 2020 21:57:31 +0200 Subject: [PATCH] =?UTF-8?q?Regioninndeling:=20Changing=20variable=20name?= =?UTF-8?q?=20for=20year=20from=20"=C3=A5r"=20to=20"aar"=20to=20avod=20cod?= =?UTF-8?q?ing=20trouble.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/no_regioninndeling.R | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/R/no_regioninndeling.R b/R/no_regioninndeling.R index 24d9813..21a60ab 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_ } -- GitLab