Home > OS >  How to add string at beginning of variable given value of another variable in R
How to add string at beginning of variable given value of another variable in R

Time:08-25

I have a dataframe that contains geoid values and state values. Because of the original format of the data, the initial 0 was dropped from the geoid for the first few states. I need to insert a "0" at the beginning of these geoid's given the value of the state variable such that this:

Geoid      State
12345      Alabama
456789     Delaware

Becomes this:

Geoid     State
012345    Alabama
456789    Delaware

For all geoid values that are less than 6 characters in length.

CodePudding user response:

you didn't provide a dataframe so I will treat Geoid as strings...

library(data.table)
library(stringr)
dt <- data.table(Geoid = c("12345", "456789"), State = c("Alabama", "Delaware"))

states <- c("Alabama", "Angola")
dt[(State %in% states) & (nchar(Geoid) < 6), Geoid := str_pad(Geoid, width=6, side="left", pad="0")]

CodePudding user response:

If Geoid is numeric R will want to delete the leading 0. But with a dplyr pipeline it can be turned into a string and ifelse() can be used to check if it's less than 6 characters and paste a leading 0 to the string.

library(dplyr)
df %>% 
  mutate(Geoid = as.character(Geoid),
         Geoid = ifelse(nchar(Geoid < 6), paste0('0', Geoid), Geoid))
  • Related