data Hi, I have a large dataset like this. I want to calculate the annual mean that I can do by dplyr:
annual_mean<-
df %>%
group_by(year) %>%
summarise(across(Plot1:Plot3, ~ mean(.x, na.rm = TRUE)))
But I need to calculate the annual mean using the months commonly present over the years excluding the NA. For example, in Plot1 may be 2,4,5 and 7 months are common over the years. So, I have to calculate the annual mean only using these months. And then different common months for Plot2, Plot3.
New member! Any help is highly appreciated!
Reproducible Sample Data
> dput(head(df, 100))
structure(list(Plot1 = c(0.77, 0.75, 0.75, 0.77, 0.79, 0.8, 0.8,
0.83, 0.81, 0.85, 0.85, NA, NA, 0.69, 0.66, 0.74, 0.63, 0.65,
0.65, 0.7, 0.7, 0.7, 0.79, 0.8, 0.75, 0.82, 0.8, 0.83, 0.8, 0.82,
NA, 0.86, 0.8, 0.81, 0.78, 0.78, 0.73, 0.79, 0.74, 0.72, 0.76,
0.71, 0.71, 0.72, 0.73, 0.71, 0.73, 0.74, 0.79, 0.78, 0.78, 0.83,
0.8, 0.85, 0.86, 0.78, 0.76, 0.73, 0.72, 0.69, 0.73, 0.65, 0.63,
0.63, 0.65, 0.69, 0.76, 0.7, 0.7, 0.71, 0.72, 0.77, 0.71, 0.76,
0.77, 0.78, 0.8, 0.76, 0.76, 0.51, 0.74, 0.71, 0.73, 0.64, 0.65,
0.63, 0.61, 0.64, 0.71, 0.68, 0.74, 0.7, 0.76, 0.74, 0.71, 0.74,
0.8, 0.78, 0.79, 0.69), Plot2 = c(NA, 0.38, 0.45, 0.48, 0.76,
0.88, 0.91, 0.9, 0.89, 0.89, 0.89, 0.84, 0.82, 0.83, 0.67, 0.62,
0.58, 0.6, 0.57, NA, NA, NA, NA, 0.42, 0.48, 0.53, 0.49, 0.51,
0.66, 0.89, NA, 0.87, 0.88, 0.85, 0.85, 0.85, 0.81, 0.69, 0.67,
0.57, 0.54, NA, NA, NA, NA, NA, 0.44, 0.51, 0.48, 0.38, 0.51,
0.83, 0.91, 0.91, 0.87, 0.89, 0.84, 0.87, 0.58, 0.75, 0.61, 0.55,
0.55, 0.55, NA, 0.61, 0.6, NA, NA, NA, NA, NA, NA, 0.78, 0.87,
0.91, 0.92, 0.9, 0.9, 0.9, 0.89, 0.88, 0.88, 0.82, 0.56, 0.54,
0.54, 0.57, NA, NA, NA, NA, NA, NA, NA, 0.28, NA, 0.69, 0.8,
0.91), Plot3 = c(NA, 0.77, 0.65, 0.7, 0.61, 0.63, 0.6, 0.59,
0.55, 0.53, 0.49, 0.44, 0.45, 0.35, 0.48, 0.51, 0.7, 0.63, 0.7,
0.7, NA, NA, NA, NA, 0.38, NA, 0.67, 0.59, 0.59, 0.61, 0.56,
0.53, 0.54, 0.51, 0.32, 0.37, 0.43, 0.4, 0.67, 0.6, 0.65, 0.67,
0.72, NA, 0.72, NA, 0.75, 0.7, 0.69, 0.66, 0.62, 0.6, 0.57, 0.54,
0.54, 0.53, 0.49, 0.33, 0.31, 0.41, 0.46, 0.47, 0.52, 0.69, 0.74,
0.7, NA, NA, 0.79, NA, 0.72, 0.72, 0.64, 0.6, 0.6, 0.58, 0.58,
0.54, 0.53, 0.51, 0.51, 0.49, 0.37, 0.51, 0.48, 0.5, 0.64, 0.77,
NA, NA, 0.78, 0.77, 0.72, NA, 0.68, 0.61, 0.59, 0.62, 0.59, 0.59
), Day = c(18, 5, 21, 6, 22, 8, 24, 9, 25, 11, 27, 12, 28, 13,
29, 15, 31, 16, 2, 18, 1, 17, 2, 18, 6, 22, 7, 23, 9, 25, 10,
26, 12, 28, 13, 29, 14, 30, 16, 1, 17, 3, 19, 1, 17, 2, 18, 6,
22, 7, 23, 9, 25, 10, 26, 12, 28, 13, 29, 14, 30, 16, 1, 17,
3, 19, 1, 17, 2, 18, 6, 22, 7, 23, 9, 25, 10, 26, 12, 28, 13,
29, 14, 30, 16, 1, 17, 3, 19, 1, 17, 2, 18, 5, 21, 6, 22, 8,
24, 9), year = c(2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000,
2000, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001,
2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001,
2001, 2001, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002,
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002,
2002, 2002, 2002, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003,
2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003,
2003, 2003, 2003, 2003, 2004, 2004, 2004, 2004, 2004, 2004, 2004,
2004, 2004, 2004, 2004), months = c(2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10, 11, 12, 12, 1, 1, 2, 2, 3, 3, 4, 4,
5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 11, 11, 12, 12, 1, 1, 2, 2,
3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 11, 11, 12, 12,
1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 11,
11, 12, 12, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6)), row.names = c(NA,
-100L), class = c("tbl_df", "tbl", "data.frame"))