Попытка создания скользящих оконных сумм на основе лет , значения et c среза моей таблицы данных
dt <- structure(list(row = c("2481", "1153", "1898", "2090", "2423",
"568", "1275", "839", "421", "478", "1270", "2449", "1059", "2449",
"1686", "1620", "633", "684", "1686", "684", "1816", "2129",
"2262", "2129", "1412", "2149", "204", "792", "935", "1151",
"1855", "1816", "605", "1842", "2588", "76", "337", "515", "1764",
"1815", "1710", "203", "1360", "1456", "1239", "962", "1296",
"1851", "1978", "1978"), year = c(1984, 1985, 1986, 1986, 1986,
1987, 1987, 1988, 1989, 1989, 1994, 1994, 1994, 1994, 1994, 1994,
1994, 1994, 1994, 1994, 1995, 1995, 1995, 1995, 1995, 1995, 1995,
1995, 1995, 1995, 1995, 1995, 1996, 1996, 1996, 1996, 1996, 1996,
1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996,
1996), variable = c("X1", "X1", "X1", "X1", "X1", "X1", "X1",
"X1", "X1", "X1", "X1", "X1", "X1", "X2", "X1", "X1", "X1", "X1",
"X2", "X2", "X1", "X1", "X1", "X2", "X1", "X1", "X1", "X1", "X1",
"X1", "X1", "X2", "X1", "X1", "X1", "X1", "X1", "X1", "X1", "X1",
"X1", "X1", "X1", "X1", "X1", "X1", "X1", "X1", "X1", "X2"),
value = c("Sh", "Sh", "Sh", "Sh", "Sh", "Sh", "SM", "Sh",
"Sh", "SM", "Ap", "De", "El", "El", "Ga", "Ic", "SA", "St",
"St", "Th", "Au", "Bi", "El", "El", "Pe", "SA", "St", "St",
"St", "St", "St", "St", "Ap", "Ap", "Ap", "Au", "Ca", "Ca",
"Ca", "Ca", "Co", "El", "El", "El", "ES", "Ic", "Ic", "Ic",
"MC", "Mi"), valueCount = c(8L, 8L, 8L, 8L, 8L, 8L, 2L, 8L,
8L, 2L, 4L, 1L, 7L, 7L, 1L, 4L, 2L, 8L, 8L, 1L, 2L, 1L, 7L,
7L, 1L, 2L, 8L, 8L, 8L, 8L, 8L, 8L, 4L, 4L, 4L, 2L, 4L, 4L,
4L, 4L, 1L, 7L, 7L, 7L, 1L, 4L, 4L, 4L, 1L, 1L), yearlycount = c(1L,
1L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 6L, 6L, 6L, 6L, 6L,
6L, 3L, 3L, 3L, 1L, 4L, 4L, 4L, 4L, 1L, 3L, 3L, 3L, 1L, 3L,
3L, 3L, 1L, 1L), yeartotal = c(1L, 1L, 3L, 3L, 3L, 2L, 2L,
1L, 2L, 2L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L)), row.names = c(NA, -50L
), .Names = c("row", "year", "variable", "value", "valueCount",
"yearlycount", "yeartotal"), class = "data.frame")
Я хочу вычислить сумму для каждого ' значения ' в данном году на основе столбца ' yearcount ' за 3 предыдущих года, как показано в ' sum-ycount ' Кроме того, Я также хочу рассчитать ' годовой итог ' каждый ' год ' на основе суммы за 3 предыдущих года, как показано на рисунке: * sum-ytotal '
year value sum-ycount sum-ytotal
1987 Sh 5 5
1987 SM 0 0
1988 Sh 5 6
1989 Sh 5 6
1989 SM 1 2
1994 Ap 0 0