Seafood-Globalization-Lab / artis-model

The ARTIS model codebase
Creative Commons Zero v1.0 Universal
1 stars 0 forks source link

Use `stringr::str_pad()` to fill out HS6 codes #23

Open theamarks opened 1 week ago

theamarks commented 1 week ago

minor code update - will save several lengthy mutate if_else() statements throughout numerous scritps

library(stringr)
str_pad(c("30112", "51122", "123456"), width = 6, pad = "0")

[1] "030112" "051122" "123456"

      # pad hs6 with 0s
      mutate(hs6 = as.character(hs6)) %>%
      mutate(hs6 = if_else(
        str_detect(hs6, "^30"),
        true = str_replace(hs6, pattern = "^30", replacement = "030"),
        if_else(str_detect(hs6, "^511"),
                true = str_replace(hs6, pattern = "^511", replacement = "0511"),
                false = hs6)))