I have the following data frame:
structure(list(name = c("jones", "williams", "jones",
"williams", "williams", "jones", "williams", "williams", "jones",
"williams", "williams", "jones", "williams", "williams", "jones",
"williams", "williams", "jones", "williams", "williams", "jones",
"williams", "jones", "jones", "jones", "jones",
"jones", "jones", "jones", "jones"), state = c("NY",
"NC", "NY", "NC", "TX", "NY", "NC", "TX", "NY", "TX", "NC", "NY",
"TX", "NC", "NY", "TX", "NC", "NY", "TX", "NC", "NY", "NC", "NY",
"NY", "NY", "NY", "NY", "NY", "NY", "NY"), year = structure(c(1995,
1995, 1996, 1996, 1996, 1997, 1997, 1997, 1998, 1998, 1998, 1999,
1999, 1999, 2000, 2000, 2000, 2001, 2001, 2001, 2002, 2002, 2003,
2004, 2005, 2006, 2007, 2008, 2009, 2010), format.stata = "%8.0g"),
year_of_election = c(NA, 1992, NA, 1992, 1996, NA, 1992,
1996, NA, 1998, 1998, 1999, 1998, 1998, 1999, 1998, 1998,
1999, 1998, 1998, 1999, 1998, 1999, 1999, 1999, 1999, 1999,
1999, 2009, 2009)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -30L), groups = structure(list(
name = c("williams", "williams", "jones"), state = c("NC",
"TX", "NY"), .rows = structure(list(c(2L, 4L, 7L, 11L, 14L,
17L, 20L, 22L), c(5L, 8L, 10L, 13L, 16L, 19L), c(1L, 3L,
6L, 9L, 12L, 15L, 18L, 21L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -3L), .drop = TRUE, class = c("tbl_df",
"tbl", "data.frame")))
我想设立一个新的一栏,称为“选举年份”,该栏将取代本年度选举的下一个学年。 因此,例如,在纽约市-纽约-1995年,“不选举年”应为4年,因为1999年是选举的下一年。 关于纽约-1999年,我想是零。 而到2000年,明年将是2009年。 关于纽约-2009年,我想是零。 因此。 如果没有“快”年,我就希望成为美国人。
这是我目前正在使用的这一法典,它偏离了相反的方向。
df <- df %>%
arrange(name, state, year) %>%
group_by(name, state) %>%
mutate(
years_until_reelection = lead(year_of_election) - year
) %>%
ungroup()