R/oktmo_restore.R

#' @title Restore oktmo value
#' @description Rewrite oktmo value in dataframe if it is less than 8 character. It often whenotmo value recognizes as numeric, not as character
#' @param df dataframe
#' @param name_col character string like 'location', 'oktmo' - name of column in dataframe with oktmo records
#' @usage oktmo_restore(df, name_col = 'oktmo')
#' @return dataframes where oktmo is character vector and consist with 8 digits
#' @export
#' @importFrom dplyr %>%
#' @importFrom dplyr mutate
#' @importFrom dplyr across
#' @importFrom tidyr matches
#' @importFrom stringr str_length
#' @examples
#' df <- tibble (location = c('56789', '1076600'))
#' oktmo_restore(df,name_col = 'location')

oktmo_restore <- function (df,      #датафрем в котором нужно привести в порядок колонку
                           name_col = 'oktmo' #имя колонки
){
  df %>%
    mutate( across(matches(name_col), ~ifelse(str_length(.) %in% c('4','5'), paste0(.,"000"), .)),
            across(matches(name_col), ~ifelse(str_length(.) == 10          , paste0("0", . ), .)),
            across(matches(name_col), ~ifelse(str_length(.) == 7           , paste0("0", . ), .)))
}
St-Digital-Twin/Dtwin documentation built on Jan. 1, 2022, 8:11 p.m.