按组计算列中 1 出现的间隔年数

Mal*_*One 4 r dataframe dplyr

我已将这两个代码建议应用到我的原始数据集来比较peace1 和peace2。

和平1:

d_muslim <- d_muslim %>% 
   mutate(last_conflict = lag(if_else(conflict == 1, year,NA),default = min(year) - 1),
   .by = country) %>%     
   tidyr::fill(last_conflict, .direction = "down") %>% 
   mutate(peace1 = year - last_conflict - 1)
Run Code Online (Sandbox Code Playgroud)

和平2:

d_muslim <- d_muslim %>%
  mutate(helper = cumsum(lag(conflict, default = 1) == 1),.by = country) %>% 
  mutate(peace2 = year - first(year), .by = c(country, helper)) %>%
  select(-helper)
Run Code Online (Sandbox Code Playgroud)

结果:

国家 冲突 和平1 和平2
伊朗 1946年 0 -73 0
伊朗 1947年 0 -72 1
伊朗 1948年 0 -71 2
伊朗 1949年 0 -70 3
伊朗 1950年 0 -69 4
伊朗 1951年 0 -68 5
伊朗 1952年 0 -67 6
伊朗 1953年 0 -66 7
伊朗 1954年 0 -65 8
伊朗 1955年 0 -64 9
伊朗 1956年 0 -63 10
伊朗 1957年 0 -62 11
伊朗 1958年 0 -61 12
伊朗 1959年 0 -60 13
伊朗 1960年 0 -59 14
伊朗 1961年 0 -58 15
伊朗 1962年 0 -57 16
伊朗 1963年 0 -56 17 号
伊朗 1964年 0 -55 18
伊朗 1965年 0 -54 19
伊朗 1966年 1 -1 0
伊朗 1967年 0 0 1
伊朗 1968年 0 1 2
伊朗 1969年 0 2 3
伊朗 1970年 0 3 4
伊朗 1971年 0 4 5
伊朗 1972年 0 5 6
伊朗 1973年 0 6 7
伊朗 1974年 0 7 8
伊朗 1975年 0 8 9
伊朗 1976年 0 9 10
伊朗 1977年 0 10 11
伊朗 1978年 0 11 12
伊朗 1979年 1 -1 0
伊朗 1980年 0 0 1
伊朗 1981年 0 1 2
伊朗 1982年 0 2 3
伊朗 1983年 0 3 4
伊朗 1984年 0 4 5
伊朗 1985年 0 5 6
伊朗 1986年 1 -1 0
伊朗 1987年 0 0 1
伊朗 1988年 0 1 2
伊朗 1989年 0 2 3
伊朗 1990年 1 -1 0
伊朗 1991年 1 -1 0
伊朗 1992年 0 0 1
伊朗 1993年 1 -1 0
伊朗 1994年 0 0 1
伊朗 1995年 0 1 2
伊朗 1996年 1 -1 0
伊朗 1997年 1 -1 0
伊朗 1998年 0 0 1
伊朗 1999年 1 -1 0
伊朗 2000年 0 0 1
伊朗 2001年 0 1 2
伊朗 2002年 0 2 3
伊朗 2003年 0 3 4
伊朗 2004年 0 4 5
伊朗 2005年 1 -1 0
伊朗 2006年 0 0 1
伊朗 2007年 0 1 2
伊朗 2008年 0 2 3
伊朗 2009年 0 3 4
伊朗 2010年 0 4 5
伊朗 2011年 0 5 6
伊朗 2012年 0 6 7
伊朗 2013年 0 7 8
伊朗 2014年 0 8 9
伊朗 2015年 0 9 10
伊朗 2016年 1 -1 0
伊朗 2017年 1 -1 0
伊朗 2018年 1 -1 0

这两个代码都不满足我的条件。

  1. 和平年的积累必须延续到冲突年。并且不能设置为0。
  2. 冲突一年后的一年必须以和平== 0开始。正如peace1函数正确执行的那样。

r2e*_*ans 5

我认为你的 15 应该是 18,但否则......

使用您的“预期”作为起点(用于并排比较):

quux |>
  mutate(
    last_conflict = lag(if_else(conflict == 1, year, NA), default = min(year) - 1),
    .by = country) |>
  tidyr::fill(last_conflict, .direction = "down") |>
  mutate(peace2 = year - last_conflict - 1)
#     country year conflict peace last_conflict peace2
# 1  country1 1990        1     0          1989      0
# 2  country1 1991        0     0          1990      0
# 3  country1 1992        0     1          1990      1
# 4  country1 1993        0     2          1990      2
# 5  country1 1994        1     3          1990      3
# 6  country1 1995        1     0          1994      0
# 7  country1 1996        0     0          1995      0
# 8  country1 1997        0     1          1995      1
# 9  country1 1998        0     2          1995      2
# 10 country1 1999        0     3          1995      3
# 11 country1 2014        0    15          1995     18
# 12 country2 1990        0     0          1989      0
# 13 country2 1991        1     1          1989      1
# 14 country2 1992        0     0          1991      0
# 15 country2 1995        1     3          1991      3
# 16 country2 1996        0     0          1995      0
# 17 country2 2000        1     4          1995      4
Run Code Online (Sandbox Code Playgroud)

数据

quux <- structure(list(country = c("country1", "country1", "country1", "country1", "country1", "country1", "country1", "country1", "country1", "country1", "country1", "country2", "country2", "country2", "country2", "country2", "country2"), year = c(1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2014L, 1990L, 1991L, 1992L, 1995L, 1996L, 2000L), conflict = c(1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L), peace = c(0L, 0L, 1L, 2L, 3L, 0L, 0L, 1L, 2L, 3L, 15L, 0L,  1L, 0L, 3L, 0L, 4L)), class = "data.frame", row.names = c(NA, -17L))
Run Code Online (Sandbox Code Playgroud)