用时间序列 R 中特定日期的平均值替换 NA
Replace NAs with mean for a particular day in time series R
鉴于下面的时间序列,我想用时间序列中 day
的所有值的 average
替换 specific day
的 NAs
。
例如,如果 1997-04-01 有缺失值,我想用时间序列中所有 4 月 1 日的平均值替换该 NA。
欢迎任何想法。
structure(list(Date = c("1997-1-1", "1997-1-2", "1997-1-3", "1997-1-4",
"1997-1-5", "1997-1-6", "1997-1-7", "1997-1-8", "1997-1-9", "1997-1-10",
"1997-1-11", "1997-1-12", "1997-1-13", "1997-1-14", "1997-1-15",
"1997-1-16", "1997-1-17", "1997-1-18", "1997-1-19", "1997-1-20",
"1997-1-21", "1997-1-22", "1997-1-23", "1997-1-24", "1997-1-25",
"1997-1-26", "1997-1-27", "1997-1-28", "1997-1-29", "1997-1-30",
"1997-1-31", "1997-2-1", "1997-2-2", "1997-2-3", "1997-2-4",
"1997-2-5", "1997-2-6", "1997-2-7", "1997-2-8", "1997-2-9", "1997-2-10",
"1997-2-11", "1997-2-12", "1997-2-13", "1997-2-14", "1997-2-15",
"1997-2-16", "1997-2-17", "1997-2-18", "1997-2-19", "1997-2-20",
"1997-2-21", "1997-2-22", "1997-2-23", "1997-2-24", "1997-2-25",
"1997-2-26", "1997-2-27", "1997-2-28", "1997-3-1", "1997-3-2",
"1997-3-3", "1997-3-4", "1997-3-5", "1997-3-6", "1997-3-7", "1997-3-8",
"1997-3-9", "1997-3-10", "1997-3-11", "1997-3-12", "1997-3-13",
"1997-3-14", "1997-3-15", "1997-3-16", "1997-3-17", "1997-3-18",
"1997-3-19", "1997-3-20", "1997-3-21", "1997-3-22", "1997-3-23",
"1997-3-24", "1997-3-25", "1997-3-26", "1997-3-27", "1997-3-28",
"1997-3-29", "1997-3-30", "1997-3-31", "1997-4-1", "1997-4-2",
"1997-4-3", "1997-4-4", "1997-4-5", "1997-4-6", "1997-4-7", "1997-4-8",
"1997-4-9", "1997-4-10", "1997-4-11", "1997-4-12", "1997-4-13",
"1997-4-14", "1997-4-15", "1997-4-16", "1997-4-17", "1997-4-18",
"1997-4-19", "1997-4-20", "1997-4-21", "1997-4-22", "1997-4-23",
"1997-4-24", "1997-4-25", "1997-4-26", "1997-4-27", "1997-4-28",
"1997-4-29", "1997-4-30", "1997-5-1", "1997-5-2", "1997-5-3",
"1997-5-4", "1997-5-5", "1997-5-6", "1997-5-7", "1997-5-8", "1997-5-9",
"1997-5-10", "1997-5-11", "1997-5-12", "1997-5-13", "1997-5-14",
"1997-5-15", "1997-5-16", "1997-5-17", "1997-5-18", "1997-5-19",
"1997-5-20", "1997-5-21", "1997-5-22", "1997-5-23", "1997-5-24",
"1997-5-25", "1997-5-26", "1997-5-27", "1997-5-28", "1997-5-29",
"1997-5-30", "1997-5-31", "1997-6-1", "1997-6-2", "1997-6-3",
"1997-6-4", "1997-6-5", "1997-6-6", "1997-6-7", "1997-6-8", "1997-6-9",
"1997-6-10", "1997-6-11", "1997-6-12", "1997-6-13", "1997-6-14",
"1997-6-15", "1997-6-16", "1997-6-17", "1997-6-18", "1997-6-19",
"1997-6-20", "1997-6-21", "1997-6-22", "1997-6-23", "1997-6-24",
"1997-6-25", "1997-6-26", "1997-6-27", "1997-6-28", "1997-6-29",
"1997-6-30", "1997-7-1", "1997-7-2", "1997-7-3", "1997-7-4",
"1997-7-5", "1997-7-6", "1997-7-7", "1997-7-8", "1997-7-9", "1997-7-10",
"1997-7-11", "1997-7-12", "1997-7-13", "1997-7-14", "1997-7-15",
"1997-7-16", "1997-7-17", "1997-7-18", "1997-7-19", "1997-7-20",
"1997-7-21", "1997-7-22", "1997-7-23", "1997-7-24", "1997-7-25",
"1997-7-26", "1997-7-27", "1997-7-28", "1997-7-29", "1997-7-30",
"1997-7-31", "1997-8-1", "1997-8-2", "1997-8-3", "1997-8-4",
"1997-8-5", "1997-8-6", "1997-8-7", "1997-8-8", "1997-8-9", "1997-8-10",
"1997-8-11", "1997-8-12", "1997-8-13", "1997-8-14", "1997-8-15",
"1997-8-16", "1997-8-17", "1997-8-18", "1997-8-19", "1997-8-20",
"1997-8-21", "1997-8-22", "1997-8-23", "1997-8-24", "1997-8-25",
"1997-8-26", "1997-8-27", "1997-8-28", "1997-8-29", "1997-8-30",
"1997-8-31", "1997-9-1", "1997-9-2", "1997-9-3", "1997-9-4",
"1997-9-5", "1997-9-6", "1997-9-7", "1997-9-8", "1997-9-9", "1997-9-10",
"1997-9-11", "1997-9-12", "1997-9-13", "1997-9-14", "1997-9-15",
"1997-9-16", "1997-9-17", "1997-9-18", "1997-9-19", "1997-9-20",
"1997-9-21", "1997-9-22", "1997-9-23", "1997-9-24", "1997-9-25",
"1997-9-26", "1997-9-27", "1997-9-28", "1997-9-29", "1997-9-30",
"1997-10-1", "1997-10-2", "1997-10-3", "1997-10-4", "1997-10-5",
"1997-10-6", "1997-10-7", "1997-10-8", "1997-10-9", "1997-10-10",
"1997-10-11", "1997-10-12", "1997-10-13", "1997-10-14", "1997-10-15",
"1997-10-16", "1997-10-17", "1997-10-18", "1997-10-19", "1997-10-20",
"1997-10-21", "1997-10-22", "1997-10-23", "1997-10-24", "1997-10-25",
"1997-10-26", "1997-10-27", "1997-10-28", "1997-10-29", "1997-10-30",
"1997-10-31", "1997-11-1", "1997-11-2", "1997-11-3", "1997-11-4",
"1997-11-5", "1997-11-6", "1997-11-7", "1997-11-8", "1997-11-9",
"1997-11-10", "1997-11-11", "1997-11-12", "1997-11-13", "1997-11-14",
"1997-11-15", "1997-11-16", "1997-11-17", "1997-11-18", "1997-11-19",
"1997-11-20", "1997-11-21", "1997-11-22", "1997-11-23", "1997-11-24",
"1997-11-25", "1997-11-26", "1997-11-27", "1997-11-28", "1997-11-29",
"1997-11-30", "1997-12-1", "1997-12-2", "1997-12-3", "1997-12-4",
"1997-12-5", "1997-12-6", "1997-12-7", "1997-12-8", "1997-12-9",
"1997-12-10", "1997-12-11", "1997-12-12", "1997-12-13", "1997-12-14",
"1997-12-15", "1997-12-16", "1997-12-17", "1997-12-18", "1997-12-19",
"1997-12-20", "1997-12-21", "1997-12-22", "1997-12-23", "1997-12-24",
"1997-12-25", "1997-12-26", "1997-12-27", "1997-12-28", "1997-12-29",
"1997-12-30", "1997-12-31", "1998-1-1", "1998-1-2", "1998-1-3",
"1998-1-4", "1998-1-5", "1998-1-6", "1998-1-7", "1998-1-8", "1998-1-9",
"1998-1-10", "1998-1-11", "1998-1-12", "1998-1-13", "1998-1-14",
"1998-1-15", "1998-1-16", "1998-1-17", "1998-1-18", "1998-1-19",
"1998-1-20", "1998-1-21", "1998-1-22", "1998-1-23", "1998-1-24",
"1998-1-25", "1998-1-26", "1998-1-27", "1998-1-28", "1998-1-29",
"1998-1-30", "1998-1-31", "1998-10-1", "1998-10-2", "1998-10-3",
"1998-10-4", "1998-10-5", "1998-10-6", "1998-10-7", "1998-10-8",
"1998-10-9", "1998-10-10", "1998-10-11", "1998-10-12", "1998-10-13",
"1998-10-14", "1998-10-15", "1998-10-16", "1998-10-17", "1998-10-18",
"1998-10-19", "1998-10-20", "1998-10-21", "1998-10-22", "1998-10-23",
"1998-10-24", "1998-10-25", "1998-10-26", "1998-10-27", "1998-10-28",
"1998-10-29", "1998-10-30", "1998-10-31", "1998-11-1", "1998-11-2",
"1998-11-3", "1998-11-4", "1998-11-5", "1998-11-6", "1998-11-7",
"1998-11-8", "1998-11-9", "1998-11-10", "1998-11-11", "1998-11-12",
"1998-11-13", "1998-11-14", "1998-11-15", "1998-11-16", "1998-11-17",
"1998-11-18", "1998-11-19", "1998-11-20", "1998-11-21", "1998-11-22",
"1998-11-23", "1998-11-24", "1998-11-25", "1998-11-26", "1998-11-27",
"1998-11-28", "1998-11-29", "1998-11-30", "1998-12-1", "1998-12-2",
"1998-12-3", "1998-12-10", "1998-12-11", "1998-12-12", "1998-12-13",
"1998-12-14", "1998-12-15", "1998-12-16", "1998-12-17", "1998-12-18",
"1998-12-19", "1998-12-20", "1998-12-21", "1998-12-22", "1998-12-23",
"1998-12-24", "1998-12-25", "1998-12-26", "1998-12-27", "1998-12-28",
"1998-12-29", "1998-12-30", "1998-12-31"), Pr = c("0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "12.0", "2.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "1.0", "1.0", "0.0",
"0.0", "0.0", "0.0", "13.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"6.0", "2.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "6.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "1.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "6.0", "6.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "9.0", "1.5", "0.0", "0.0", NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "6.0", "9.0", "0.0", "0.0", "0.0", "4.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "10.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "8.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "30.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "16.0", "0.0", "15.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "3.5", "0.0", "0.0", "11.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "2.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "11.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "2.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "10.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "10.0", "0.0", "6.0", "19.8",
"27.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"2.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"2.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "1.5", "21.5", "1.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "4.5", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "7.0", "0.0", "0.0",
"0.0", "1.0", "0.0", "0.0", "30.6", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "6.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"4.5", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0"), Tmax = c(21.5,
22, 25, 16, 17.5, 22, 22.6, 23.5, 22, 22, 21.5, 22, 19, 19.5,
19, 20, 19.5, 17, 13.5, 12, 14.5, 18, 16.5, 19.5, 21, 20, 13.5,
16, 20.5, 18.5, 20, 23, 15.5, 16, 18, 19.5, 21.5, 22, 20.5, 15.5,
18.5, 20.5, 21, 21, 22, 21.5, 22, 23, 25, 24.5, 23.5, 23, 26,
27, 26.5, 21, 25.6, 25, 25.5, 24.5, 21, 21, 25, 27, 26.5, 24,
21, 21.5, 25, 20, 28, 26, 26.5, 25, 21, 24, 19, 16, 21, 26, 25.6,
24.5, 25, 29, 30, 29.5, 25, 19, 26.5, 26, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 29, 34, 25, 26.5, 30, 33.5,
33.5, 23, 25.5, 28.5, 32, 29, 29.5, 25, 32.5, 36, 35, 35.5, 35.5,
37, 37, 40, 39.5, 40, 39.5, 39, 39, 41, 41, 40.5, 35, 33, 34,
35.5, 36.5, 31, 37, 37.5, 36.5, 37.5, 36, 37.5, 39, 40, 35, 40,
43, 42.5, 45, 46, 42.5, 38.5, 36.5, 41.5, 42, 42, 41.5, 42.5,
37, 36, 36, 36.5, 39, 41, 40.5, 41.5, 44, 43, 42.5, 42, 41.5,
39.5, 38, 39.5, 40.5, 38.5, 39, 39, 39.5, 41.5, 37, 33, 33.5,
35, 37.5, 38.5, 38.5, 38.5, 31.3, 30.2, 31, 36, 37.5, 37.5, 37.5,
38.5, 38.5, 39, 40, 39.5, 34, 38, 38.5, 29, 33.5, 36, 37, 40,
39.5, 39, 40.5, 39, 39, 36.5, 30, 33.5, 38.5, 34, 32, 29, 35.5,
37, 37.5, 37.5, 38.5, 38, 36, 37, 37, 36, 37.5, 33.5, 35, 35.5,
35, 35.5, 34, 37, 36, 30, 38, 40, 36, 36.5, 33.5, 35, 37, 35,
36.5, 37, 35, 34, 30.5, 32, 30.5, 32.5, 30, 30, 30, 32.5, 29,
31, 32, 30.5, 31, 30.5, 30.5, 29, 29.5, 28, 24.5, 25.5, 26.5,
18, 16.5, 23.5, 26.5, 28, 23.5, 23.5, 26.5, 26.5, 27, 23, 28.5,
26.5, 29, 26.5, 27, 26.5, 27.5, 28, 22, 19.5, 22, 24, 26, 23,
23, 22, 22, 26, 26, 23.5, 25, 25, 25, 22.6, 22.5, 16.5, 15, 18,
21, 21, 23.5, 21, 20, 19, 21, 21.5, 21.5, 20, 20.5, 20.5, 16,
16, 13, 14.5, 19, 15.5, 20, 19, 20.5, 19.5, 22, 22, 12, 21.5,
21, 18, 17, 12.5, 10, 12.5, 15.5, 16, 19, 17, 22, 22.5, 18.5,
15, 18.5, 21.5, 14, 13.5, 16.5, 17, 9.5, 14, 17, 16.5, 18, 19,
17.5, 18.5, 17, 17.5, 18.5, 21, 22, 22, 19, 20, 14.5, 14.5, 36.5,
33.5, 35.3, 34, 35, 34, 35, 35.5, 36, 34, 29.5, 35.5, 35.5, 33.5,
32, 31.5, 28, 28, 32, 31, 33.5, 35.5, 33, 32.5, 31.5, 31.5, 27,
30.5, 31, 30.5, 31, 33, 31.5, 29.5, 28, 29, 27, 28.5, 24.5, 28,
27.5, 30, 29, 27.5, 27, 26.5, 28, 27, 27.5, 28, 27.5, 27, 28.5,
28.5, 27, 27.5, 27.5, 25.5, 25.5, 27.5, 24.5, 22.5, 26, 25.5,
25.5, 25, 20, 20.5, 18, 17, 18, 18, 19, 19.5, 10.5, 19.5, 20.5,
20.5, 19.5, 22.5, 19.5, 21.5, 20.5, 19.5, 21.5, 16.5), Tmin = c(3,
3, 4.5, 5.5, 5, 5, 3, 2, 1, 2, 1, 1, 3, 1.5, 1.5, 1, 1.5, 5.5,
7.5, 2, 2, 2.6, 1, 1.5, 1, 2, 2, 2.5, 3.5, 3, 3.5, 4, 6, 3, 3.5,
4, 3, 4, 4.5, 2, 3, 3, 3.5, 4, 6, 4, 5, 4.5, 5, 4.5, 4, 6, 6,
7, 9, 8.5, 9.5, 7, 8, 12, 9, 5.5, 6, 9, 10, 14, 12.5, 13, 12,
10.5, 8, 9, 9, 12, 10.5, 11, 11.5, 9, 8, 9, 9.5, 11, 10, 11,
14, 14.5, 14, 12, 10, 15, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 18, 19.5, 19, 16.5, 17, 18, 17.5, 13.5, 12,
15, 18.5, 13.6, 17, 13.5, 16.5, 16.5, 19.5, 19, 18, 19, 19.5,
20, 21.5, 21.5, 21.5, 23.5, 23.5, 24, 23, 23, 23, 20, 24, 22,
21.5, 21.5, 23.5, 24.5, 25, 24.5, 23, 25, 25, 24, 24, 25, 25,
25, 26, 27.5, 22, 22.5, 26, 25.5, 25.5, 26.5, 26.5, 25.5, 26.5,
25, 24, 25, 26.5, 27.5, 24.5, 26.5, 28, 30, 31, 30, 28.5, 28.5,
28, 28, 30.5, 29.5, 29.5, 29.5, 30, 31, 25.5, 27, 23.5, 25, 27.5,
28.5, 29, 29, 26.5, 26, 25, 22, 25.5, 23, 24.5, 24.5, 26, 24,
27.5, 28.6, 28, 28.5, 29, 25.5, 23, 24.5, 25.5, 27.5, 28.5, 28,
29, 29.5, 28.5, 24, 24, 24, 26, 26, 21.5, 21.5, 22.5, 23, 24,
25.5, 26.5, 24.5, 26.5, 28, 27.5, 27, 24, 23.5, 24.5, 26, 26,
23, 23.5, 25.5, 26.5, 22, 24, 24.5, 25, 21, 22, 23, 22.5, 23,
24, 20, 20, 19, 18.5, 20, 18.5, 20, 19.5, 19, 19.5, 18.5, 18,
19, 19, 16, 17, 17, 18.5, 17, 16.5, 18, 16.5, 14, 15, 15, 13.5,
13.5, 13, 14.5, 16.5, 15, 13.5, 13.6, 15, 11.5, 11.5, 12, 12.5,
12.5, 12, 12, 12, 13, 12.5, 12.5, 9, 9, 6.5, 9, 9.5, 12.5, 9,
9.5, 7, 7.5, 9, 7, 8, 8, 8.5, 11, 9, 7, 7, 6.5, 5, 5.5, 8.5,
7.5, 8, 7.5, 6.5, 6.5, 7, 4.5, 5, 8.5, 10, 4.5, 4.6, 7.4, 5.5,
5, 5, 5, 5, 5.4, 5, 4, 3.5, 4, 4, 6, 5, 0, 0.5, 1, 1, 1.9, 4.9,
6, 6.5, 6.5, 8.5, 5.5, 5, 5.5, 5.5, 3, 4, 5.5, 0.5, 0.5, 1, 0.5,
0.9, 1, 1.9, 3.5, 3.5, 2.5, 3, 2.9, 4.9, 5.9, 6, 9, 21, 21.5,
22, 23, 20.5, 21.5, 21, 21.5, 19, 18, 16.5, 14, 16, 16.5, 16.5,
16, 16.5, 17, 17, 16.5, 15.5, 15.5, 15.5, 14.5, 15, 16, 14.5,
14, 14, 12.5, 12, 10, 11, 10, 10, 10.5, 11.9, 14.4, 14, 11, 10.5,
11, 11, 11, 11, 11, 9.5, 7, 7.5, 7.5, 8, 7, 7, 7.5, 7.9, 8, 8,
7.5, 7.5, 6, 6.5, 5, 5, 6, 4.5, 4, 4.5, 3.5, 3.5, 2.5, 2.5, 3,
3.5, 3, 2.5, 3, 2.5, 2, 2.5, 2, 3.5, 2.5, 2.9, 3.4, 3.5, 3)), row.names = c(NA,
-482L), class = "data.frame")
我想对于每个度量,除了日期之外,您还想创建一个修改后的列,该列要么是该值,要么在 NA 的情况下是该日期的平均值。为此,我创建了一个日期列作为分组依据,然后使用 ifelse 语句改变每个目标列,如果不是 NA 则给出值,如果 NA 则按天取平均值。
您的数据没有任何天数的 non-date 列具有相同天数的 NA 值,因此要在此处测试修改后的数据:
dat <- tibble(
Date = c("1997-4-1", "1997-4-1", "1998-4-1", "1997-4-2"),
Pr = c(NA, 1, 2, 3),
Tmax = c(1,NA,3, 3),
Tmin = c(1,2,3,4))
这里是要修改的代码:
library(dplyr)
dat %>%
mutate(day = sub(".*?-", "", Date)) %>%
group_by(day) %>%
mutate(across(.cols = c(Pr, Tmax, Tmin), ~ifelse(is.na(.x), mean(.x, na.rm = T), .x), .names = "modified_{.col}"))
这是结果。您可以取消分组以进行进一步处理:
# A tibble: 4 x 8
# Groups: day [2]
Date Pr Tmax Tmin day modified_Pr modified_Tmax modified_Tmin
<chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 1997-4-1 NA 1 1 4-1 1.5 1 1
2 1997-4-1 1 NA 2 4-1 1 2 2
3 1998-4-1 2 3 3 4-1 2 3 3
4 1997-4-2 3 3 4 4-2 3 3 4
鉴于下面的时间序列,我想用时间序列中 day
的所有值的 average
替换 specific day
的 NAs
。
例如,如果 1997-04-01 有缺失值,我想用时间序列中所有 4 月 1 日的平均值替换该 NA。
欢迎任何想法。
structure(list(Date = c("1997-1-1", "1997-1-2", "1997-1-3", "1997-1-4",
"1997-1-5", "1997-1-6", "1997-1-7", "1997-1-8", "1997-1-9", "1997-1-10",
"1997-1-11", "1997-1-12", "1997-1-13", "1997-1-14", "1997-1-15",
"1997-1-16", "1997-1-17", "1997-1-18", "1997-1-19", "1997-1-20",
"1997-1-21", "1997-1-22", "1997-1-23", "1997-1-24", "1997-1-25",
"1997-1-26", "1997-1-27", "1997-1-28", "1997-1-29", "1997-1-30",
"1997-1-31", "1997-2-1", "1997-2-2", "1997-2-3", "1997-2-4",
"1997-2-5", "1997-2-6", "1997-2-7", "1997-2-8", "1997-2-9", "1997-2-10",
"1997-2-11", "1997-2-12", "1997-2-13", "1997-2-14", "1997-2-15",
"1997-2-16", "1997-2-17", "1997-2-18", "1997-2-19", "1997-2-20",
"1997-2-21", "1997-2-22", "1997-2-23", "1997-2-24", "1997-2-25",
"1997-2-26", "1997-2-27", "1997-2-28", "1997-3-1", "1997-3-2",
"1997-3-3", "1997-3-4", "1997-3-5", "1997-3-6", "1997-3-7", "1997-3-8",
"1997-3-9", "1997-3-10", "1997-3-11", "1997-3-12", "1997-3-13",
"1997-3-14", "1997-3-15", "1997-3-16", "1997-3-17", "1997-3-18",
"1997-3-19", "1997-3-20", "1997-3-21", "1997-3-22", "1997-3-23",
"1997-3-24", "1997-3-25", "1997-3-26", "1997-3-27", "1997-3-28",
"1997-3-29", "1997-3-30", "1997-3-31", "1997-4-1", "1997-4-2",
"1997-4-3", "1997-4-4", "1997-4-5", "1997-4-6", "1997-4-7", "1997-4-8",
"1997-4-9", "1997-4-10", "1997-4-11", "1997-4-12", "1997-4-13",
"1997-4-14", "1997-4-15", "1997-4-16", "1997-4-17", "1997-4-18",
"1997-4-19", "1997-4-20", "1997-4-21", "1997-4-22", "1997-4-23",
"1997-4-24", "1997-4-25", "1997-4-26", "1997-4-27", "1997-4-28",
"1997-4-29", "1997-4-30", "1997-5-1", "1997-5-2", "1997-5-3",
"1997-5-4", "1997-5-5", "1997-5-6", "1997-5-7", "1997-5-8", "1997-5-9",
"1997-5-10", "1997-5-11", "1997-5-12", "1997-5-13", "1997-5-14",
"1997-5-15", "1997-5-16", "1997-5-17", "1997-5-18", "1997-5-19",
"1997-5-20", "1997-5-21", "1997-5-22", "1997-5-23", "1997-5-24",
"1997-5-25", "1997-5-26", "1997-5-27", "1997-5-28", "1997-5-29",
"1997-5-30", "1997-5-31", "1997-6-1", "1997-6-2", "1997-6-3",
"1997-6-4", "1997-6-5", "1997-6-6", "1997-6-7", "1997-6-8", "1997-6-9",
"1997-6-10", "1997-6-11", "1997-6-12", "1997-6-13", "1997-6-14",
"1997-6-15", "1997-6-16", "1997-6-17", "1997-6-18", "1997-6-19",
"1997-6-20", "1997-6-21", "1997-6-22", "1997-6-23", "1997-6-24",
"1997-6-25", "1997-6-26", "1997-6-27", "1997-6-28", "1997-6-29",
"1997-6-30", "1997-7-1", "1997-7-2", "1997-7-3", "1997-7-4",
"1997-7-5", "1997-7-6", "1997-7-7", "1997-7-8", "1997-7-9", "1997-7-10",
"1997-7-11", "1997-7-12", "1997-7-13", "1997-7-14", "1997-7-15",
"1997-7-16", "1997-7-17", "1997-7-18", "1997-7-19", "1997-7-20",
"1997-7-21", "1997-7-22", "1997-7-23", "1997-7-24", "1997-7-25",
"1997-7-26", "1997-7-27", "1997-7-28", "1997-7-29", "1997-7-30",
"1997-7-31", "1997-8-1", "1997-8-2", "1997-8-3", "1997-8-4",
"1997-8-5", "1997-8-6", "1997-8-7", "1997-8-8", "1997-8-9", "1997-8-10",
"1997-8-11", "1997-8-12", "1997-8-13", "1997-8-14", "1997-8-15",
"1997-8-16", "1997-8-17", "1997-8-18", "1997-8-19", "1997-8-20",
"1997-8-21", "1997-8-22", "1997-8-23", "1997-8-24", "1997-8-25",
"1997-8-26", "1997-8-27", "1997-8-28", "1997-8-29", "1997-8-30",
"1997-8-31", "1997-9-1", "1997-9-2", "1997-9-3", "1997-9-4",
"1997-9-5", "1997-9-6", "1997-9-7", "1997-9-8", "1997-9-9", "1997-9-10",
"1997-9-11", "1997-9-12", "1997-9-13", "1997-9-14", "1997-9-15",
"1997-9-16", "1997-9-17", "1997-9-18", "1997-9-19", "1997-9-20",
"1997-9-21", "1997-9-22", "1997-9-23", "1997-9-24", "1997-9-25",
"1997-9-26", "1997-9-27", "1997-9-28", "1997-9-29", "1997-9-30",
"1997-10-1", "1997-10-2", "1997-10-3", "1997-10-4", "1997-10-5",
"1997-10-6", "1997-10-7", "1997-10-8", "1997-10-9", "1997-10-10",
"1997-10-11", "1997-10-12", "1997-10-13", "1997-10-14", "1997-10-15",
"1997-10-16", "1997-10-17", "1997-10-18", "1997-10-19", "1997-10-20",
"1997-10-21", "1997-10-22", "1997-10-23", "1997-10-24", "1997-10-25",
"1997-10-26", "1997-10-27", "1997-10-28", "1997-10-29", "1997-10-30",
"1997-10-31", "1997-11-1", "1997-11-2", "1997-11-3", "1997-11-4",
"1997-11-5", "1997-11-6", "1997-11-7", "1997-11-8", "1997-11-9",
"1997-11-10", "1997-11-11", "1997-11-12", "1997-11-13", "1997-11-14",
"1997-11-15", "1997-11-16", "1997-11-17", "1997-11-18", "1997-11-19",
"1997-11-20", "1997-11-21", "1997-11-22", "1997-11-23", "1997-11-24",
"1997-11-25", "1997-11-26", "1997-11-27", "1997-11-28", "1997-11-29",
"1997-11-30", "1997-12-1", "1997-12-2", "1997-12-3", "1997-12-4",
"1997-12-5", "1997-12-6", "1997-12-7", "1997-12-8", "1997-12-9",
"1997-12-10", "1997-12-11", "1997-12-12", "1997-12-13", "1997-12-14",
"1997-12-15", "1997-12-16", "1997-12-17", "1997-12-18", "1997-12-19",
"1997-12-20", "1997-12-21", "1997-12-22", "1997-12-23", "1997-12-24",
"1997-12-25", "1997-12-26", "1997-12-27", "1997-12-28", "1997-12-29",
"1997-12-30", "1997-12-31", "1998-1-1", "1998-1-2", "1998-1-3",
"1998-1-4", "1998-1-5", "1998-1-6", "1998-1-7", "1998-1-8", "1998-1-9",
"1998-1-10", "1998-1-11", "1998-1-12", "1998-1-13", "1998-1-14",
"1998-1-15", "1998-1-16", "1998-1-17", "1998-1-18", "1998-1-19",
"1998-1-20", "1998-1-21", "1998-1-22", "1998-1-23", "1998-1-24",
"1998-1-25", "1998-1-26", "1998-1-27", "1998-1-28", "1998-1-29",
"1998-1-30", "1998-1-31", "1998-10-1", "1998-10-2", "1998-10-3",
"1998-10-4", "1998-10-5", "1998-10-6", "1998-10-7", "1998-10-8",
"1998-10-9", "1998-10-10", "1998-10-11", "1998-10-12", "1998-10-13",
"1998-10-14", "1998-10-15", "1998-10-16", "1998-10-17", "1998-10-18",
"1998-10-19", "1998-10-20", "1998-10-21", "1998-10-22", "1998-10-23",
"1998-10-24", "1998-10-25", "1998-10-26", "1998-10-27", "1998-10-28",
"1998-10-29", "1998-10-30", "1998-10-31", "1998-11-1", "1998-11-2",
"1998-11-3", "1998-11-4", "1998-11-5", "1998-11-6", "1998-11-7",
"1998-11-8", "1998-11-9", "1998-11-10", "1998-11-11", "1998-11-12",
"1998-11-13", "1998-11-14", "1998-11-15", "1998-11-16", "1998-11-17",
"1998-11-18", "1998-11-19", "1998-11-20", "1998-11-21", "1998-11-22",
"1998-11-23", "1998-11-24", "1998-11-25", "1998-11-26", "1998-11-27",
"1998-11-28", "1998-11-29", "1998-11-30", "1998-12-1", "1998-12-2",
"1998-12-3", "1998-12-10", "1998-12-11", "1998-12-12", "1998-12-13",
"1998-12-14", "1998-12-15", "1998-12-16", "1998-12-17", "1998-12-18",
"1998-12-19", "1998-12-20", "1998-12-21", "1998-12-22", "1998-12-23",
"1998-12-24", "1998-12-25", "1998-12-26", "1998-12-27", "1998-12-28",
"1998-12-29", "1998-12-30", "1998-12-31"), Pr = c("0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "12.0", "2.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "1.0", "1.0", "0.0",
"0.0", "0.0", "0.0", "13.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"6.0", "2.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "6.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "1.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "6.0", "6.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "9.0", "1.5", "0.0", "0.0", NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "6.0", "9.0", "0.0", "0.0", "0.0", "4.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "10.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "8.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "30.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "16.0", "0.0", "15.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "3.5", "0.0", "0.0", "11.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "2.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "11.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "2.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "10.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "10.0", "0.0", "6.0", "19.8",
"27.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"2.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"2.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "1.5", "21.5", "1.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "4.5", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "7.0", "0.0", "0.0",
"0.0", "1.0", "0.0", "0.0", "30.6", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "6.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"4.5", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0",
"0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0"), Tmax = c(21.5,
22, 25, 16, 17.5, 22, 22.6, 23.5, 22, 22, 21.5, 22, 19, 19.5,
19, 20, 19.5, 17, 13.5, 12, 14.5, 18, 16.5, 19.5, 21, 20, 13.5,
16, 20.5, 18.5, 20, 23, 15.5, 16, 18, 19.5, 21.5, 22, 20.5, 15.5,
18.5, 20.5, 21, 21, 22, 21.5, 22, 23, 25, 24.5, 23.5, 23, 26,
27, 26.5, 21, 25.6, 25, 25.5, 24.5, 21, 21, 25, 27, 26.5, 24,
21, 21.5, 25, 20, 28, 26, 26.5, 25, 21, 24, 19, 16, 21, 26, 25.6,
24.5, 25, 29, 30, 29.5, 25, 19, 26.5, 26, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 29, 34, 25, 26.5, 30, 33.5,
33.5, 23, 25.5, 28.5, 32, 29, 29.5, 25, 32.5, 36, 35, 35.5, 35.5,
37, 37, 40, 39.5, 40, 39.5, 39, 39, 41, 41, 40.5, 35, 33, 34,
35.5, 36.5, 31, 37, 37.5, 36.5, 37.5, 36, 37.5, 39, 40, 35, 40,
43, 42.5, 45, 46, 42.5, 38.5, 36.5, 41.5, 42, 42, 41.5, 42.5,
37, 36, 36, 36.5, 39, 41, 40.5, 41.5, 44, 43, 42.5, 42, 41.5,
39.5, 38, 39.5, 40.5, 38.5, 39, 39, 39.5, 41.5, 37, 33, 33.5,
35, 37.5, 38.5, 38.5, 38.5, 31.3, 30.2, 31, 36, 37.5, 37.5, 37.5,
38.5, 38.5, 39, 40, 39.5, 34, 38, 38.5, 29, 33.5, 36, 37, 40,
39.5, 39, 40.5, 39, 39, 36.5, 30, 33.5, 38.5, 34, 32, 29, 35.5,
37, 37.5, 37.5, 38.5, 38, 36, 37, 37, 36, 37.5, 33.5, 35, 35.5,
35, 35.5, 34, 37, 36, 30, 38, 40, 36, 36.5, 33.5, 35, 37, 35,
36.5, 37, 35, 34, 30.5, 32, 30.5, 32.5, 30, 30, 30, 32.5, 29,
31, 32, 30.5, 31, 30.5, 30.5, 29, 29.5, 28, 24.5, 25.5, 26.5,
18, 16.5, 23.5, 26.5, 28, 23.5, 23.5, 26.5, 26.5, 27, 23, 28.5,
26.5, 29, 26.5, 27, 26.5, 27.5, 28, 22, 19.5, 22, 24, 26, 23,
23, 22, 22, 26, 26, 23.5, 25, 25, 25, 22.6, 22.5, 16.5, 15, 18,
21, 21, 23.5, 21, 20, 19, 21, 21.5, 21.5, 20, 20.5, 20.5, 16,
16, 13, 14.5, 19, 15.5, 20, 19, 20.5, 19.5, 22, 22, 12, 21.5,
21, 18, 17, 12.5, 10, 12.5, 15.5, 16, 19, 17, 22, 22.5, 18.5,
15, 18.5, 21.5, 14, 13.5, 16.5, 17, 9.5, 14, 17, 16.5, 18, 19,
17.5, 18.5, 17, 17.5, 18.5, 21, 22, 22, 19, 20, 14.5, 14.5, 36.5,
33.5, 35.3, 34, 35, 34, 35, 35.5, 36, 34, 29.5, 35.5, 35.5, 33.5,
32, 31.5, 28, 28, 32, 31, 33.5, 35.5, 33, 32.5, 31.5, 31.5, 27,
30.5, 31, 30.5, 31, 33, 31.5, 29.5, 28, 29, 27, 28.5, 24.5, 28,
27.5, 30, 29, 27.5, 27, 26.5, 28, 27, 27.5, 28, 27.5, 27, 28.5,
28.5, 27, 27.5, 27.5, 25.5, 25.5, 27.5, 24.5, 22.5, 26, 25.5,
25.5, 25, 20, 20.5, 18, 17, 18, 18, 19, 19.5, 10.5, 19.5, 20.5,
20.5, 19.5, 22.5, 19.5, 21.5, 20.5, 19.5, 21.5, 16.5), Tmin = c(3,
3, 4.5, 5.5, 5, 5, 3, 2, 1, 2, 1, 1, 3, 1.5, 1.5, 1, 1.5, 5.5,
7.5, 2, 2, 2.6, 1, 1.5, 1, 2, 2, 2.5, 3.5, 3, 3.5, 4, 6, 3, 3.5,
4, 3, 4, 4.5, 2, 3, 3, 3.5, 4, 6, 4, 5, 4.5, 5, 4.5, 4, 6, 6,
7, 9, 8.5, 9.5, 7, 8, 12, 9, 5.5, 6, 9, 10, 14, 12.5, 13, 12,
10.5, 8, 9, 9, 12, 10.5, 11, 11.5, 9, 8, 9, 9.5, 11, 10, 11,
14, 14.5, 14, 12, 10, 15, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 18, 19.5, 19, 16.5, 17, 18, 17.5, 13.5, 12,
15, 18.5, 13.6, 17, 13.5, 16.5, 16.5, 19.5, 19, 18, 19, 19.5,
20, 21.5, 21.5, 21.5, 23.5, 23.5, 24, 23, 23, 23, 20, 24, 22,
21.5, 21.5, 23.5, 24.5, 25, 24.5, 23, 25, 25, 24, 24, 25, 25,
25, 26, 27.5, 22, 22.5, 26, 25.5, 25.5, 26.5, 26.5, 25.5, 26.5,
25, 24, 25, 26.5, 27.5, 24.5, 26.5, 28, 30, 31, 30, 28.5, 28.5,
28, 28, 30.5, 29.5, 29.5, 29.5, 30, 31, 25.5, 27, 23.5, 25, 27.5,
28.5, 29, 29, 26.5, 26, 25, 22, 25.5, 23, 24.5, 24.5, 26, 24,
27.5, 28.6, 28, 28.5, 29, 25.5, 23, 24.5, 25.5, 27.5, 28.5, 28,
29, 29.5, 28.5, 24, 24, 24, 26, 26, 21.5, 21.5, 22.5, 23, 24,
25.5, 26.5, 24.5, 26.5, 28, 27.5, 27, 24, 23.5, 24.5, 26, 26,
23, 23.5, 25.5, 26.5, 22, 24, 24.5, 25, 21, 22, 23, 22.5, 23,
24, 20, 20, 19, 18.5, 20, 18.5, 20, 19.5, 19, 19.5, 18.5, 18,
19, 19, 16, 17, 17, 18.5, 17, 16.5, 18, 16.5, 14, 15, 15, 13.5,
13.5, 13, 14.5, 16.5, 15, 13.5, 13.6, 15, 11.5, 11.5, 12, 12.5,
12.5, 12, 12, 12, 13, 12.5, 12.5, 9, 9, 6.5, 9, 9.5, 12.5, 9,
9.5, 7, 7.5, 9, 7, 8, 8, 8.5, 11, 9, 7, 7, 6.5, 5, 5.5, 8.5,
7.5, 8, 7.5, 6.5, 6.5, 7, 4.5, 5, 8.5, 10, 4.5, 4.6, 7.4, 5.5,
5, 5, 5, 5, 5.4, 5, 4, 3.5, 4, 4, 6, 5, 0, 0.5, 1, 1, 1.9, 4.9,
6, 6.5, 6.5, 8.5, 5.5, 5, 5.5, 5.5, 3, 4, 5.5, 0.5, 0.5, 1, 0.5,
0.9, 1, 1.9, 3.5, 3.5, 2.5, 3, 2.9, 4.9, 5.9, 6, 9, 21, 21.5,
22, 23, 20.5, 21.5, 21, 21.5, 19, 18, 16.5, 14, 16, 16.5, 16.5,
16, 16.5, 17, 17, 16.5, 15.5, 15.5, 15.5, 14.5, 15, 16, 14.5,
14, 14, 12.5, 12, 10, 11, 10, 10, 10.5, 11.9, 14.4, 14, 11, 10.5,
11, 11, 11, 11, 11, 9.5, 7, 7.5, 7.5, 8, 7, 7, 7.5, 7.9, 8, 8,
7.5, 7.5, 6, 6.5, 5, 5, 6, 4.5, 4, 4.5, 3.5, 3.5, 2.5, 2.5, 3,
3.5, 3, 2.5, 3, 2.5, 2, 2.5, 2, 3.5, 2.5, 2.9, 3.4, 3.5, 3)), row.names = c(NA,
-482L), class = "data.frame")
我想对于每个度量,除了日期之外,您还想创建一个修改后的列,该列要么是该值,要么在 NA 的情况下是该日期的平均值。为此,我创建了一个日期列作为分组依据,然后使用 ifelse 语句改变每个目标列,如果不是 NA 则给出值,如果 NA 则按天取平均值。
您的数据没有任何天数的 non-date 列具有相同天数的 NA 值,因此要在此处测试修改后的数据:
dat <- tibble(
Date = c("1997-4-1", "1997-4-1", "1998-4-1", "1997-4-2"),
Pr = c(NA, 1, 2, 3),
Tmax = c(1,NA,3, 3),
Tmin = c(1,2,3,4))
这里是要修改的代码:
library(dplyr)
dat %>%
mutate(day = sub(".*?-", "", Date)) %>%
group_by(day) %>%
mutate(across(.cols = c(Pr, Tmax, Tmin), ~ifelse(is.na(.x), mean(.x, na.rm = T), .x), .names = "modified_{.col}"))
这是结果。您可以取消分组以进行进一步处理:
# A tibble: 4 x 8
# Groups: day [2]
Date Pr Tmax Tmin day modified_Pr modified_Tmax modified_Tmin
<chr> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 1997-4-1 NA 1 1 4-1 1.5 1 1
2 1997-4-1 1 NA 2 4-1 1 2 2
3 1998-4-1 2 3 3 4-1 2 3 3
4 1997-4-2 3 3 4 4-2 3 3 4