I am trying to pull apart a numeric field into two parts so I can merge with another data set. The number of characters is 6 or 7 characters long. I was able to get the last characters easily enough, but now I need either the first one or two characters of the field, depending on how long it is. This was my stab at it, but I received a number of errors. Is it possible to nest an If statement like this? Or do I need to swap it and run an If statement in a loop with different assignment statements?
df$new_field <- as.numeric(substr(df$GEOID, 1 ,if(nchar(df$GEOID)=6){
return(1)
}
else{
return(2)
}))
With can use ifelse
instead of if/else
as ifelse
is vectorized while if/else
takes only a single value of length 1 and returns length 1
df$new_field <- with(df, as.numeric(substr(GEOID, 1,
ifelse(nchar(GEOID) == 6, 1, 2))))
Or another option is to convert the logical vector to numeric
n <- (nchar(df$GEOID) != 6) + 1
df$new_field <- with(df, as.numeric(substr(GEOID, 1, n)))
Using a reproducible example
v1 <- c('1234567', '123456', '1234')
n <- (nchar(v1) != 6) + 1
n
#[1] 2 1 2
substr(v1, 1, n)
#[1] "12" "1" "12"
NOTE: In addition to substr
, substring
can also be used with the same method