mardi 25 août 2020

Nested IF statement to create new column in R

I'm new at R, and trying to learn. Here is a much reduced df I'm working with. Pretend this is about 90 locations all using city abbreviations

df$Location <- c("SEA", "DEN", "CHI", "NYC")

I am trying to add a column called TimeZone by using this formula

if(df$Location==c('SEA')){
  df$TimeZone <- "PST"
} else {
  if(df$Facility==c('DEN')){
  df$TimeZone <- "MST"
} else {
  if(df$Location==c('CHI')){
  df$TimeZone <- "CST"
} else {
 if(df$Location==c('NYC')){
  df$Location <- "EST"
  }
  }
  }  
}

I am receiving this error:

longer object length is not a multiple of shorter object lengththe condition has length > 1 and only the first element will be usedlonger object length is not a multiple of shorter object lengththe condition has length > 1 and only the first element will be usedlonger object length is not a multiple of shorter object lengththe condition has length > 1 and only the first element will be used

Aucun commentaire:

Enregistrer un commentaire