Я не совсем уверен, что это то, что вы ищете, дайте мне знать, если это ближе к тому, что вы имели в виду.
library(data.table)
library(zoo)
df <- data.frame(
name = c("Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria"),
year = c(1800, 1800, 1800, 1801, 1801, 1801),
value = c(11, 22, 6, 48, 60, 120),
month = 1
)
cols <- c("month", "value")
res <- setDT(df)[, .SD[match(1:12, month)], by = .(name, year)]
res[, month := seq(.N), by = .(name, year)]
res[, (cols) := lapply(.SD, na.approx, na.rm = FALSE), .SDcols = cols, by = name]
Вывод
name year value month
1: Afghanistan 1800 11.00000 1
2: Afghanistan 1800 14.08333 2
3: Afghanistan 1800 17.16667 3
4: Afghanistan 1800 20.25000 4
5: Afghanistan 1800 23.33333 5
6: Afghanistan 1800 26.41667 6
7: Afghanistan 1800 29.50000 7
8: Afghanistan 1800 32.58333 8
9: Afghanistan 1800 35.66667 9
10: Afghanistan 1800 38.75000 10
11: Afghanistan 1800 41.83333 11
12: Afghanistan 1800 44.91667 12
13: Albania 1800 22.00000 1
14: Albania 1800 25.16667 2
15: Albania 1800 28.33333 3
16: Albania 1800 31.50000 4
17: Albania 1800 34.66667 5
18: Albania 1800 37.83333 6
19: Albania 1800 41.00000 7
20: Albania 1800 44.16667 8
21: Albania 1800 47.33333 9
22: Albania 1800 50.50000 10
23: Albania 1800 53.66667 11
24: Albania 1800 56.83333 12
25: Algeria 1800 6.00000 1
26: Algeria 1800 15.50000 2
27: Algeria 1800 25.00000 3
28: Algeria 1800 34.50000 4
29: Algeria 1800 44.00000 5
30: Algeria 1800 53.50000 6
31: Algeria 1800 63.00000 7
32: Algeria 1800 72.50000 8
33: Algeria 1800 82.00000 9
34: Algeria 1800 91.50000 10
35: Algeria 1800 101.00000 11
36: Algeria 1800 110.50000 12
37: Afghanistan 1801 48.00000 1
...
Данные
df <- data.frame(
name = c("Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria"),
year = c(1800, 1800, 1800, 1801, 1801, 1801),
value = c(11, 22, 6, 48, 60, 120),
month = 1
)