samedi 29 juin 2019

How to substitute in R "Ethiopia" with "Ethiopia (-1992)" and "Ethiopia (1993-)" based on the year?

I am trying to substitute "Ethiopia" in location_1 with "Ethiopia (-1992)" if location_1 says "Ethiopia" and the years correspond to all years up to and including 1992 and with "Ethiopia (1993-)" if location_1 says "Ethiopia" and the years correspond to all years from 1993 forward.

Unfortunately, the code I came up with substitutes all with "Ethiopia (-1992)" even for those years after 1992.

The following is the code:

if (mydata$year >= 1992) {
  mydata$location_1 <- sub("Ethiopia", "Ethiopia (-1992)", mydata$location_1)
} else mydata$location_1 <- sub("Ethiopia", "Ethiopia (1993-)", mydata$location_1)

I was hoping that I would have all "Ethiopia" turned into either "Ethiopia (-1992)" or "Ethiopia (1993-)" based on the year. Instead, the results are that all "Ethiopia" become "Ethiopia (-1992)".

Aucun commentaire:

Enregistrer un commentaire