for, . -hoc- ( 0 ), , :
Take an example of a zip code column. All values must contain 5 digits (for example, 01234), but R deletes the leading zeros (so "01234" becomes "1234"). You can add trailing zero to all cells that contain only 4 characters with this code:
for (i in 1:nrow(df)){
if(nchar(df$zipCode[i])<5){
df$zipCode[i]<- paste0('0',df$zipCode[i])
}
}
source
share