Creation of a new variable with condition

Hi!

In my dataset I have the variable "pko_dummy" which measures the presence of a peace mission (0 = no PKO, 1 = PKO). I also have the variables "year" (from 1990 to 2018) and "cown", which assigns a standard numerical value to the countries considered (African countries). I would now like to create a new variable that marks for the various countries the year in which they first experienced a peace mission. In the case where it was not present, I would like an NA to be assigned. If for example the country "402" hosted a peacekeeping mission from 2013 to 2016, then I would like the new variable to report the year "2013" for as many times as the country is present in the dataset. What should I do?

Here below a reproducible sample of the data:

data <- structure(list(cown = c(432, 432, 432, 432, 432, 432, 432, 432, 
                                432, 432), 
                       year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 
                                                    1997, 1998, 1999), 
                       intensity_level = c(1, 1, 0, 0, 1, 0, 0, 0, 
                                           0, 0), 
                       pa_dummy = c(0, 1, 1, 0, 0, 0, 0, 0, 0, 0), 
                       pko_dummy = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
                       parl_wom.per = c(NA, NA, 0.023, 0.023, 0.023, 0.023, 
                                        0.023, 0.122449, 0.122449, 0.122449), 
                       exe_wom.per = c(0.0588235, 0.1052632, 0.0526316, 
                                       0.0952381, 0.1111111, 0.0555556, 
                                       0.125, 0.1176471, 0.2608696, 0.2727273), 
                       gender_mean = c(0, 0, 1.75, 0, 0, 0, 0, 0, 0, 0), 
                       gender_art = c(0, 0, 7, 0, 0, 0, 0, 0, 0, 0), 
                       female_pko.per = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
                       lf_wom.per = c(0.60855, 0.60834, 0.6082, 0.60815, 
                                      0.6082, 0.60838, 0.60806, 0.60798, 
                                      0.60804, 0.60811), 
                       ss.per = c(0.0679799, 0.0723098, 0.0827134, 0.0837933, 
                                  0.0957365, 0.1073224, 0.1127522, 0.1228388, 
                                  0.1336761, 0.1510765), 
                       fdi.per = c(0.0021364, 0.0004424, -0.0077276, 0.001441, 
                                   0.0083661, 0.0411724, 0.009786, 0.0275705, 
                                   0.0032724, 0.0090061), 
                       ele.sy = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
                       polity = c(-7, NA, 4, 4, 4, 4, 4, 5, 5, 5), 
                       mus.per = c(0.944, 0.944, 0.944, 0.944, 0.944, 
                                   0.944, 0.944, 0.944, 0.944, 0.944), 
                       cons_ref = c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1), 
                       jud_ind.per = c(0.4763113, 0.5237863, 0.5575284, 
                                       0.548066, 0.548066, 0.548066, 0.548066, 
                                       0.548066, 0.548066, 0.548066)), 
                  row.names = c(NA, -10L), class = "data.frame")

You haven't provided example data with more than one country code, with any pko_dummy event ... so testing my proposed solution would be not as trivial as it could have been, and so I've take no effort to test/prove my solution. But here iis what I came up with, if its not right it probably wont be all that far off.
I use library(tidyverse), and will assume your data.frame is called data1 rather than data (as I don't like my names to clash).

group_by(data1,cown) %>% 
mutate(first_pko = min(ifelse(pko_dummy,year,NA),na.rm=TRUE)) %>% 
ungroup()

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.