I want to replace the missing value with the average of past years on the same date.
I think for this it is worth using the dplyr package from R's tidyverse to group the data by month and day. How can I conduct mean imputation on a subset of data?
DATA <- read.csv('DateCaratNO2.csv')
DATA <- as.data.frame(DATA)
DATA$Full.Data <- as.POSIXct(DATA$date, format = "%m/%d/%Y")
DATA$day <- format(DATA$Full.Data, "%d")
DATA$month <- format(DATA$Full.Data, "%m")
DATA$year <- format(DATA$Full.Data, "%Y")
attach(DATA)
library(dplyr)
df <- DATA %>% mutate(day = lubridate::floor_date(Full.Data, "day"),
month = lubridate::floor_date(Full.Data, "month")) %>%
dplyr::group_by(day, month, ID) %>%
mutate(NO2 = replace_na(NO2, mean(NO2, na.rm=TRUE)))
I need to replace the missing value with the average value for the same day and month for a specific station. Any help is appreciated!
The dput
from the data can be found here:
structure(list(ID = c("IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A", "IT1940A",
"IT1940A", "IT1940A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A", "IT1938A",
"IT1938A", "IT1938A"), date = c("2/1/2015", "2/2/2015", "2/3/2015",
"2/4/2015", "2/5/2015", "2/6/2015", "2/7/2015", "2/8/2015", "2/9/2015",
"2/10/2015", "2/11/2015", "2/12/2015", "2/13/2015", "2/14/2015",
"2/15/2015", "2/16/2015", "2/17/2015", "2/18/2015", "2/19/2015",
"2/20/2015", "2/21/2015", "2/22/2015", "2/23/2015", "2/24/2015",
"2/25/2015", "2/26/2015", "2/27/2015", "2/28/2015", "2/1/2016",
"2/2/2016", "2/3/2016", "2/4/2016", "2/5/2016", "2/6/2016", "2/7/2016",
"2/8/2016", "2/9/2016", "2/10/2016", "2/11/2016", "2/12/2016",
"2/13/2016", "2/14/2016", "2/15/2016", "2/16/2016", "2/17/2016",
"2/18/2016", "2/19/2016", "2/20/2016", "2/21/2016", "2/22/2016",
"2/23/2016", "2/24/2016", "2/25/2016", "2/26/2016", "2/27/2016",
"2/28/2016", "2/29/2016", "1/1/2017", "1/2/2017", "1/3/2017",
"1/4/2017", "1/5/2017", "1/6/2017", "1/7/2017", "1/8/2017", "1/9/2017",
"1/10/2017", "1/11/2017", "1/12/2017", "1/13/2017", "1/14/2017",
"1/15/2017", "1/16/2017", "1/17/2017", "1/18/2017", "1/19/2017",
"1/20/2017", "1/21/2017", "1/22/2017", "1/23/2017", "1/24/2017",
"1/25/2017", "1/26/2017", "1/27/2017", "1/28/2017", "1/29/2017",
"1/30/2017", "1/31/2017", "2/1/2018", "2/2/2018", "2/3/2018",
"2/4/2018", "2/5/2018", "2/6/2018", "2/7/2018", "2/8/2018", "2/9/2018",
"2/10/2018", "2/11/2018", "2/12/2018", "2/13/2018", "2/14/2018",
"2/15/2018", "2/16/2018", "2/17/2018", "2/18/2018", "2/19/2018",
"2/20/2018", "2/21/2018", "2/22/2018", "2/23/2018", "2/24/2018",
"2/25/2018", "2/26/2018", "2/27/2018", "2/28/2018", "1/1/2019",
"1/2/2019", "1/3/2019", "1/4/2019", "1/5/2019", "1/6/2019", "1/7/2019",
"1/8/2019", "1/9/2019", "1/10/2019", "1/11/2019", "1/12/2019",
"1/13/2019", "1/14/2019", "1/15/2019", "1/16/2019", "1/17/2019",
"1/18/2019", "1/19/2019", "1/20/2019", "1/21/2019", "1/22/2019",
"1/23/2019", "1/24/2019", "1/25/2019", "1/26/2019", "1/27/2019",
"1/28/2019", "1/29/2019", "1/30/2019", "1/31/2019", "2/1/2019",
"2/2/2019", "2/3/2019", "2/4/2019", "2/5/2019", "2/6/2019", "2/7/2019",
"2/8/2019", "2/9/2019", "2/10/2019", "2/11/2019", "2/12/2019",
"2/13/2019", "2/14/2019", "2/15/2019", "2/16/2019", "2/17/2019",
"2/18/2019", "2/19/2019", "2/20/2019", "2/21/2019", "2/22/2019",
"2/23/2019", "2/24/2019", "2/25/2019", "2/26/2019", "2/27/2019",
"2/28/2019", "1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020",
"1/5/2020", "1/6/2020", "1/7/2020", "1/8/2020", "1/9/2020", "1/10/2020",
"1/11/2020", "1/12/2020", "1/13/2020", "1/14/2020", "1/15/2020",
"1/16/2020", "1/17/2020", "1/18/2020", "1/19/2020", "1/20/2020",
"1/21/2020", "1/22/2020", "1/23/2020", "1/24/2020", "1/25/2020",
"1/26/2020", "1/27/2020", "1/28/2020", "1/29/2020", "1/30/2020",
"1/31/2020", "2/1/2021", "2/2/2021", "2/3/2021", "2/4/2021",
"2/5/2021", "2/6/2021", "2/7/2021", "2/8/2021", "2/9/2021", "2/10/2021",
"2/11/2021", "2/12/2021", "2/13/2021", "2/14/2021", "2/15/2021",
"2/16/2021", "2/17/2021", "2/18/2021", "2/19/2021", "2/20/2021",
"2/21/2021", "2/22/2021", "2/23/2021", "2/24/2021", "2/25/2021",
"2/26/2021", "2/27/2021", "2/28/2021", "2/1/2015", "2/2/2015",
"2/3/2015", "2/4/2015", "2/5/2015", "2/6/2015", "2/7/2015", "2/8/2015",
"2/9/2015", "2/10/2015", "2/11/2015", "2/12/2015", "2/13/2015",
"2/14/2015", "2/15/2015", "2/16/2015", "2/17/2015", "2/18/2015",
"2/19/2015", "2/20/2015", "2/21/2015", "2/22/2015", "2/23/2015",
"2/24/2015", "2/25/2015", "2/26/2015", "2/27/2015", "2/28/2015",
"2/1/2016", "2/2/2016", "2/3/2016", "2/4/2016", "2/5/2016", "2/6/2016",
"2/7/2016", "2/8/2016", "2/9/2016", "2/10/2016", "2/11/2016",
"2/12/2016", "2/13/2016", "2/14/2016", "2/15/2016", "2/16/2016",
"2/17/2016", "2/18/2016", "2/19/2016", "2/20/2016", "2/21/2016",
"2/22/2016", "2/23/2016", "2/24/2016", "2/25/2016", "2/26/2016",
"2/27/2016", "2/28/2016", "2/29/2016", "1/1/2017", "1/2/2017",
"1/3/2017", "1/4/2017", "1/5/2017", "1/6/2017", "1/7/2017", "1/8/2017",
"1/9/2017", "1/10/2017", "1/11/2017", "1/12/2017", "1/13/2017",
"1/14/2017", "1/15/2017", "1/16/2017", "1/17/2017", "1/18/2017",
"1/19/2017", "1/20/2017", "1/21/2017", "1/22/2017", "1/23/2017",
"1/24/2017", "1/25/2017", "1/26/2017", "1/27/2017", "1/28/2017",
"1/29/2017", "1/30/2017", "1/31/2017", "2/1/2018", "2/2/2018",
"2/3/2018", "2/4/2018", "2/5/2018", "2/6/2018", "2/7/2018", "2/8/2018",
"2/9/2018", "2/10/2018", "2/11/2018", "2/12/2018", "2/13/2018",
"2/14/2018", "2/15/2018", "2/16/2018", "2/17/2018", "2/18/2018",
"2/19/2018", "2/20/2018", "2/21/2018", "2/22/2018", "2/23/2018",
"2/24/2018", "2/25/2018", "2/26/2018", "2/27/2018", "2/28/2018",
"1/1/2019", "1/2/2019", "1/3/2019", "1/4/2019", "1/5/2019", "1/6/2019",
"1/7/2019", "1/8/2019", "1/9/2019", "1/10/2019", "1/11/2019",
"1/12/2019", "1/13/2019", "1/14/2019", "1/15/2019", "1/16/2019",
"1/17/2019", "1/18/2019", "1/19/2019", "1/20/2019", "1/21/2019",
"1/22/2019", "1/23/2019", "1/24/2019", "1/25/2019", "1/26/2019",
"1/27/2019", "1/28/2019", "1/29/2019", "1/30/2019", "1/31/2019",
"2/1/2019", "2/2/2019", "2/3/2019", "2/4/2019", "2/5/2019", "2/6/2019",
"2/7/2019", "2/8/2019", "2/9/2019", "2/10/2019", "2/11/2019",
"2/12/2019", "2/13/2019", "2/14/2019", "2/15/2019", "2/16/2019",
"2/17/2019", "2/18/2019", "2/19/2019", "2/20/2019", "2/21/2019",
"2/22/2019", "2/23/2019", "2/24/2019", "2/25/2019", "2/26/2019",
"2/27/2019", "2/28/2019", "1/1/2020", "1/2/2020", "1/3/2020",
"1/4/2020", "1/5/2020", "1/6/2020", "1/7/2020", "1/8/2020", "1/9/2020",
"1/10/2020", "1/11/2020", "1/12/2020", "1/13/2020", "1/14/2020",
"1/15/2020", "1/16/2020", "1/17/2020", "1/18/2020", "1/19/2020",
"1/20/2020", "1/21/2020", "1/22/2020", "1/23/2020", "1/24/2020",
"1/25/2020", "1/26/2020", "1/27/2020", "1/28/2020", "1/29/2020",
"1/30/2020", "1/31/2020", "2/1/2021", "2/2/2021", "2/3/2021",
"2/4/2021", "2/5/2021", "2/6/2021", "2/7/2021", "2/8/2021", "2/9/2021",
"2/10/2021", "2/11/2021", "2/12/2021", "2/13/2021", "2/14/2021",
"2/15/2021", "2/16/2021", "2/17/2021", "2/18/2021", "2/19/2021",
"2/20/2021", "2/21/2021", "2/22/2021", "2/23/2021", "2/24/2021",
"2/25/2021", "2/26/2021", "2/27/2021", "2/28/2021"), NO2 = c(8.494022,
10.270843, 20.854183, 26.973156, 17.957637, 14.908667, 16.15965,
11.995295, 8.860629, 10.035246, 20.141964, 22.327379, 21.02741,
24.465761, 16.538571, 16.556504, 4.783193, 7.59238, 19.161681,
22.677657, 15.586068, NA, NA, NA, 16.631454, 11.29906, 14.864193,
16.420849, 14.104021, 18.786681, 16.8078, 5.788044, 4.648989,
15.276459, 6.557777, 14.57682, 21.385529, 7.954627, 9.436122,
9.503997, 6.562462, 9.555659, 14.54271, 11.293801, 9.99976, 5.034411,
6.153122, 3.590124, 3.350876, 15.858318, 11.843004, 3.834344,
13.858539, 7.778169, 8.614458, 2.18815, 14.864767, 23.718167,
22.192393, 21.003893, 11.270417, 12.869576, 5.541511, 6.065571,
8.600884, 15.744785, 17.657224, 19.806599, 21.258571, 20.145979,
9.321535, 12.00277, 15.655933, 17.461837, 24.439565, 16.996054,
13.463386, 4.692335, 2.42172, 9.528777, 8.088544, 20.124756,
17.19798, 17.549501, 17.667262, 10.398431, 20.365667, 26.515232,
13.012708, 7.710178, 6.731884, 6.873468, 13.216107, 8.248941,
12.667134, 7.967476, 13.450384, 4.469243, 6.016051, 20.221312,
9.978077, 8.125063, 6.054654, 17.886531, 16.911314, 14.872607,
18.379769, 13.280178, 13.173737, 16.163359, 7.180439, 7.067153,
5.78097, 9.115078, 11.401236, 13.672157, 7.67733, 9.121578, 9.53306,
9.169626, 10.399597, 9.929665, 13.432182, 21.290884, 11.990955,
7.629396, 11.871206, 7.8844, 13.096837, 17.530286, 15.16868,
23.066559, 24.500176, 21.301399, 19.780404, 10.435753, 17.164999,
10.730105, 10.39671, 21.078079, 10.896736, 12.381213, 17.814773,
10.77259, 12.325191, 14.441106, 13.372432, 12.20334, 2.267116,
7.004822, 6.484815, 6.160732, 11.205868, 17.221174, 12.932672,
15.424868, 13.584568, 9.045232, 9.253143, 4.746712, 5.356391,
7.956635, 15.78769, 15.203057, 21.611717, 13.938919, 10.338719,
14.906831, 12.02256, 8.323146, 5.962648, 12.607288, 8.366664,
11.722606, 13.118021, 13.167657, 23.943211, 15.73205, NA, NA,
NA, 19.458806, 13.905096, 22.780715, 28.207544, 16.330735, 10.908304,
13.027756, 18.055168, 18.981838, 14.758766, 22.892374, 15.730101,
4.159135, 6.782227, 8.725889, 16.589754, 16.98659, 14.466077,
9.835424, 5.603747, 7.533586, 7.258124, 3.757233, 6.975397, 16.506639,
22.993903, 22.146121, 32.453523, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 8.874777, 5.274195, 8.771376, NA, 13.485412, 21.698904,
27.922464, 16.498973, 28.762024, 23.60364, 24.60113, 27.767211,
31.555646, 31.960224, 12.792771, 11.874074, 25.523096, 36.005253,
37.642116, 43.185004, 26.004919, 30.18264, 25.999374, NA, 32.056592,
37.521471, 38.501752, 43.199343, 47.116269, 39.203263, 45.309426,
26.376231, 25.090029, 35.950189, 33.532655, 28.406965, 24.936877,
32.878943, 26.889793, 24.207449, 31.056998, 29.679594, 36.303335,
28.31691, 35.54293, 18.791613, 17.165094, 31.426778, 26.191531,
33.661524, NA, 18.94205, 8.852177, 20.852655, 22.10272, 17.050833,
18.940024, NA, NA, NA, NA, 22.335409, 23.47458, 12.399243, 23.636525,
NA, 21.434094, 22.799834, 23.083384, 17.963889, 9.915422, 20.821297,
35.158618, 35.247528, 33.416598, 35.369512, 34.734347, 40.436504,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 36.207926, 33.685805, 30.957001,
37.630453, 42.27948, 46.808245, 31.737096, 53.81515, 18.27784,
33.830545, 50.393628, 42.016389, 36.003148, 36.643481, 47.376107,
36.050758, 26.756718, 19.165313, 8.837838, 18.859527, 32.150087,
24.903609, 16.42232, 28.227237, 32.079918, 27.3481, NA, 33.219278,
33.551203, 35.606984, 38.398312, 45.264114, 41.581028, 24.478762,
50.790748, 32.137279, 39.643216, 28.691853, 42.126903, 33.707412,
27.494751, 26.748115, 38.457202, 37.951861, 17.364936, 32.06137,
26.519822, 41.503974, 46.908624, 30.394106, 47.184144, 35.195138,
39.95927, 41.872991, 38.210744, 27.324773, 30.350131, 52.469295,
34.62326, 32.508014, 33.325587, 29.8272, 31.795985, NA, 51.978294,
38.143252, 29.89106, 42.116386, 36.054773, 32.849115, 18.242602,
18.520913, 29.982071, 26.920768, 27.383854, 21.16106, 9.234787,
NA, 36.617475, 12.935865, 18.385505, 25.670893, 26.499172, 23.917398,
18.786183, 8.155483, 13.224367, 4.103037, 22.595824, 28.268346,
26.41122, 17.739975, 26.06706, 24.139574, 28.397405, 30.879564,
24.112995, 10.538676, 9.012384, 14.210404, 24.614131, 33.060392,
24.914316, 33.322334, 27.921317, 35.113113, 26.507393, 14.22918,
7.30602, 37.749187, 43.352686, 34.664751, 33.116411, 40.646825,
32.890795, 30.315907, 37.905017, 43.562624, 40.983335, 28.682102,
34.585785, 26.711978, 33.635901, 32.167869, 39.401156, 41.840677,
46.413608, 31.613008, 28.768333, 27.468747, 19.921701, 16.148044,
23.349917, 19.946367, 22.412272, 27.136632, 17.069591, 21.201786,
35.533755, 35.117128, 25.738005, 20.21309, 12.068008, 10.685154,
24.855044, 18.835436, 12.114451, 6.274859, 9.588431, 18.601217,
27.243322, 27.039121, 28.688985, 27.590541, 15.86111, 28.251711,
27.363969, 22.524697, 34.540087, 32.924065, 14.335373, 15.010271
)), class = "data.frame", row.names = c(NA, -468L))
CodePudding user response:
To do the mean of grouped data I would use summarise:
group_by(day, month, ID) %>%
summarise(
average = mean(value, na.rm = TRUE)
) %>%
ungroup()
Is this helping?
CodePudding user response:
Imputation Method
Another option if you want to provide a more realistic imputation is using the mice
package, which uses a much more effective method for imputing missing data. For the example I show here, I pool the data into a complete dataset. If you were to run an inferential test on this data, it would be invalid due to Rubin's Rules for imputation, so you would need to run this data with the regression, t-test, etc. before pooling the data. If you are just concerned with having realistic descriptive data this is not an issue.
How to Perform
Here is how you achieve this. First load the mice
package for imputation and the tidyverse
for plotting later.
#### Load Library ####
library(mice)
library(tidyverse)
You first impute the data. Typically the default is 5 imputations, which I have designated specifically here. I have named your dput as data
:
#### Impute Data Five Times ####
set.seed(123) # to reproduce results
imp <- mice(data = data,
m=5)
Then we can check how the imputations performed by using these two plots:
#### Check Imputations ####
plot(imp) # should be scattered
densityplot(imp) # shows densities used for each imputation
Finally, you can pool the imputed data into a complete dataset without NA values.
#### Pool Data Together ####
complete.data <- complete(imp)
Plot Complete Data
As an example, here is the NO2 data, which now has no NA values, as they have been imputed already.
#### Plot Imputed NO2 Data ####
complete.data %>%
ggplot(aes(x=NO2))
geom_density(fill="steelblue",
alpha = .4,
size=1)
labs(y="Density",
title = "Density of Imputed NO2 Data")
theme_classic()
Reference
Here are the mice
vignettes if you want to learn more about this method:
https://www.gerkovink.com/miceVignettes/
CodePudding user response:
I think this does what you want (with tidyverse and lubridate)
data %>%
mutate(
date = as.POSIXct(date, format = "%m/%d/%Y"),
day = day(date),
month = month(date),
year = year(date)
) %>%
group_by(day, month, ID) %>%
mutate(
aveNO2 = mean(NO2,na.rm=TRUE)
) %>%
ungroup() %>%
mutate(
comment = case_when(is.na(NO2) ~ "ave used"),
NO2 = case_when(is.na(NO2) ~ aveNO2,
TRUE ~ NO2)
)