Home > Mobile >  R - Change all 4 digit characters to have a 0 in front, but keep all 5 digit values the same [duplic
R - Change all 4 digit characters to have a 0 in front, but keep all 5 digit values the same [duplic

Time:10-07

I am trying to make a choropleth map on a county level. Each county has a unique id. I need to merge IDs from the dataset to the ID of the geographic info of the county. The dataset has county_id as a 4 digit number, for example 1001. The geographic data is in ID 01001. All county_ids with 5 digits are already correct.

Below is my solution, but I assume there is a better way to do this. Please teach me a way to make this more efficient.

library(tidyverse)
library(urbnmapr)
library(janitor)

broadband <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-05-11/broadband.csv') %>% 
  clean_names()

  broadband_4_digits <- broadband %>% 
  mutate(id_length = str_length(county_id)) %>% 
  filter(id_length == 4) %>% 
  mutate(county_fips = paste0("0",county_id))

broadband_5_digits <- broadband %>% 
  mutate(id_length = str_length(county_id)) %>% 
  filter(id_length == 5) %>% 
  mutate(county_fips = county_id)

broadband <- rbind(broadband_4_digits, broadband_5_digits)

broadband <- broadband %>% 
  right_join(counties, by = "county_fips")

CodePudding user response:

We can handle this via a single call to sub:

broadband$county_id <- sub("^\\d{4}$", paste0("0", broadbad$county_id), broadbad$county_id)

Or we could just use sprintf:

broadband$county_id <- sprintf("d", broadband$county_id)

CodePudding user response:

You can use the str_pad function from the stringr library:

library(stringr)
str_pad(as.character(c(1001,10001)), pad = "0",width = 5)

[1] "01001" "10001"

  •  Tags:  
  • r
  • Related