This is my first-ever posting.
I have Census Tract FIPS Codes that are 11-digit character strings. The Census Bureau CSV files have a column but some codes are only 9 or 10 digits. All observations of the code needs to be 11 characters. I have 77,000+ rows. Here is a slice of 10 rows.
DATA
b1 <- structure(list(geocode1 = c("9001990000", "9007990100", "9009990000",
"9011990100", "23005990000", "23009990000", "23013990000", "23015990000",
"23023990000", "23029990000")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
CODE
str_pad(b1, 11, pad = "0")
ERROR
str_pad(b1, 11, pad = "0")
[1] "c(\"9001990000\", \"9007990100\", \"9009990000\", \"9011990100\", \"23005990000\", \"23009990000\", \"23013990000\", \"23015990000\", \"23023990000\", \"23029990000\")"
Warning message:
In stri_pad_left(string, width, pad = pad, use_length = !use_width) :
argument is not an atomic vector; coercing
COMMENTS I tried permutations of all arguments of stringr::str_pad. I also referred to the vignettes. The vignette examples work fine, but my own code does not. I created another example that works fine:
a1 <- c(1, 2, 31, 201, 1006)
a2 <- as.character(a1)
a2
str_pad(a2, 7, pad = "0")
> str_pad(a2, 7, pad = "0")
[1] "0000001" "0000002" "0000031" "0000201" "0001006"
QUESTION How do I get all observations in the FIPS Code character vector to be 11 characters by adding leading zeroes on the left?