0

I worked on the script with a fellow student, but I am quite inexperienced with R myself.

I want to show the development of democratisation episodes from 1960 to 2021 in a bar chart, but the last three years (2018 to 2021) are never shown in the graph. We have set the conditions for testing whether an episode exists in the section shown below. My idea about the problem was the following: One of the conditions is that there is no episode if the values are stagnant over 4 years. Since the values of the 4 following years are tested for this by lag or lead, 2018-2021 are not taken into account, since they can be tested for a maximum of three years in advance. However, I have no idea whether I am on the right track with this, or how I could solve the problem.

d_dem <- d1 %>% 
group_by(iso3c) %>%
arrange(iso3c, year) %>%
mutate(
d_dem = v2x_libdem,
    d_lag = lag(d_dem),
d_diff = v2x_libdem - d_lag,
d_lead_diff1 = lead(d_diff, n = 1),
d_lead_diff2 = lead(d_diff, n = 2),
d_lead_diff3 = lead(d_diff, n = 3),
d_lead_diff4 = lead(d_diff, n = 4),
d_up01 = if_else(d_diff > 0.01, 1, 0),
  d_down02 = if_else(d_diff < -0.02, 1, 0)) %>%
ungroup %>%
rowwise() %>%
mutate(
d_change4 =
max(
 d_diff,
d_lead_diff1,
d_lead_diff2,
d_lead_diff3,
na.rm = TRUE)) %>%
ungroup %>%
mutate(
d_stag = if_else(d_change4 < .01, 1, 0) ) %>%
mutate(
d_potstart = d_up01,
d_potend = if_else(d_stag | d_down02, 1, 0)




 # I already tried to solve it by adding this: 

#     d_potend = if_else(year == 2021, 1, d_potend),
#     d_potend = if_else(between(year, 2018, 2021), 0, d_potend))
IRTFM
  • 258,963
  • 21
  • 364
  • 487

0 Answers0