Can't seem to get this 100% correct.
Let's say I have a column of zipcodes. They vary from length but the maximum they would be is 9.
ZipCodes
19425
143489
190387462
345
19429
etc etc
If the zip is length 5, it's good. If it's 9, it's good. However if it's 8, I need to pad a 0 to the left. If it's length 7, I need to pad 2, etc for each result to add to 9. I ignore 9's and 5's simply because those are already complete zips, the length of 5 population just does not have the trailing 4. I've tried a few variations and the zips can be treated as either numeric or character, it does not matter.
for(row in CPD$ZipP) {
#print(row) }
if (nchar(row) == 8) {
row <- paste0("0", row)
} else if (nchar(row) == 7) {
row <- paste0("00", row)
} else if (nchar(row) == 6) {
row <- paste0("000", row)
} else if (nchar(row) == 4) {
row <- paste0("00000", row)
} else
print("zero")
}
When I run this, all it does is print zero, so clearly there is an issue with my logic. Basically I'm trying to loop the dataset, and update each row where the length needs to be changed.