mercredi 7 octobre 2020

Why my nested ifelse is not working properly in R

I have a data frame and I am trying to create a new column based on another column's value condition. The first 4 ifelse is working fine but later 3 ifelse is not working. Also not showing any errors.

I am not sure how to write ifelse condition for the character type factor column.

The code is given below

data$Region <- ifelse(data$Station.Names == "Bogra" | data$Station.Names == "Dinajpur" | data$Station.Names == "Rangpur" | data$Station.Names == "Syedpur", "Northern Region",
                  ifelse(data$Station.Names == "Rajshah", "Northwestern Region",
                         ifelse(data$Station.Names == "Satkhira" | data$Station.Names == "Khepupara" , "Northsouthern Region",
                                ifelse(data$Station.Names == "Dhaka" | data$Station.Names == "Tangail" | data$Station.Names == "Mymensingh", "Central egion",
                                       ifelse(data$Station.Names == "Comilla" | data$Stations.Names == "Feni" | data$Stations.Names == "Maijdee-Court" | data$Stations.Names == "Madaripur", "Southern Region",
                                              ifelse(data$Station.Names == "Chittagong (IAP-Patenga)" | data$Station.Names == "Sitakunda" | data$Station.Names == "Teknaf", "Southeastern Region",
                                                     ifelse(data$Station.Names == "Sylhet" | data$Station.Names == "Srimangal", "Eastern Region", "NAA")
                                              )
                                       )
                                )
                         )
                  )

)

Any kind of guidelines is appreciated.

Aucun commentaire:

Enregistrer un commentaire