jo.magi jo.magi - 4 months ago 20
R Question

Creating a new variable from existing variables with a condition

I am trying to create a new variable from my existing variables and that it satisfies a condition.

startyear endyear ongoingasofyear imposition
(dbl) (dbl) (dbl) (dbl)
1 1945 1947 NA 1
2 1946 1949 NA 1
3 1946 1993 NA 1
4 1946 1960 NA 1
5 1946 1946 NA 1
6 1946 NA 1946 1


I want to create a variable called sanctions_period. I want it to range from startyear - endyear when those are available and then range from startyear - ongoing if endyear is NA and ongoingasofyear is not NA.

This is what it should look like:

startyear endyear ongoingasofyear imposition sanctions_period
(dbl) (dbl) (dbl) (dbl)
1 1945 1947 NA 1 1945-1947
2 1946 1949 NA 1 1946-1949
3 1946 1993 NA 1 1946-1993
4 1946 1960 NA 1 1946-1960
5 1946 1946 NA 1 1946-1946
6 1946 NA 1946 1 1946-ongoing


How might i go about doing this? I have tried different setups with ifelse, but so far haven't been successful. Thanks!

Answer

As we are using the 'tbl_df', one option with the dplyr would be to create the variable using mutate by pasteing the columns together and an ifelse condition can be used to account for the NA elements in 'endyear'.

 library(dplyr)
 df1 %>%
     mutate(sanctions_period = ifelse(is.na(endyear) & !is.na(ongoingasofyear), 
         paste(startyear, "ongoing", sep="-"), paste(startyear, endyear, sep="-")))
#     startyear endyear ongoingasofyear imposition sanctions_period
#1      1945    1947              NA          1        1945-1947
#2      1946    1949              NA          1        1946-1949
#3      1946    1993              NA          1        1946-1993
#4      1946    1960              NA          1        1946-1960
#5      1946    1946              NA          1        1946-1946
#6      1946      NA            1946          1     1946-ongoing