mercredi 29 janvier 2020

How can I nest an If statement in a substr function in R

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)
}))

Aucun commentaire:

Enregistrer un commentaire