I am new to R and posting so please forgive me if I miss some protocols, but here is my question: I am creating temporary vectors in order to add '0s' where needed. I ultimately want a value comprised of 12 digits and where this is not the case, I am going to add the amount of '0s' that I require. However, after attempting to paste my temporary indices with the appropriate zeros, I get the following message:
colnames(ALLMBRS) <- c("SSN","tracts","GeoBlock","GeoCodeBlck","GeoMatch") #TA Members Tracts
#Remove special characters and decimals
tmp1 <- str_replace_all(ALLMBRS$GeoCode,"[[:punct:]]","")
#Temporary Vector of ALLMBRS
tmp2 <- tmp1
#Vectors of Indices used to add 0's
add1 <- str_length(ALLMBRS$tracts) == 11
add2 <- str_length(ALLMBRS$tracts) == 10
add3 <- str_length(ALLMBRS$tracts) == 9
add4 <- str_length(ALLMBRS$tracts) == 8
add5 <- str_length(ALLMBRS$tracts) == 7
#Paste temporary vector indices into temporary vector
tmp2[add1] <- paste(tmp2[add1],"0",sep="")
tmp2[add2] <- paste(tmp2[add2],"00",sep="")
tmp2[add3] <- paste(tmp2[add3],"000",sep="")
tmp2[add4] <- paste(tmp2[add4],"0000",sep="")
tmp2[add5] <- paste(tmp2[add5],"00000",sep="")
Example of Data:
[1] "0" "0" "0" "0" "0" "0"
[7] "0" "360010146121" "720210310133" "0" "517100023001" "90034808002"
[13] "250158202021" "250158211004" "250138125003" "290470203002" "250138124031" "250158202033"
[19] "250138019012" "250138112002"
I expect all values to contain 12 digits. So I would like to see for
[1]000000000000
and for
[12]900348080020
Error Message: Error in tmp2[add1] <- paste(tmp2[add1],"0",sep = ""):
NAs are not allowed in subscripted assignments
If I do have NA's in my data how can I circumvent this so I can accomplish my task. Thank you for any help.