Search code examples
raveragemeanna

Need to calculate 24 hour average where NAs are considered 0


I'm trying to calculate a 24 hour average from hourly readings where NAs are not counted as part of the divisor and I'm successful in this calculation, but I'm also trying to calculate the 24 hour average from hourly readings where NAs are considered 0 and included in the divisor.

data <- data.frame(
   day = c("Mon", "Tue", "Wed"),
   hour_1 = c(4, 6, NA),
   hour_2 = c(4, 0, NA),
   hour_3 = c(4, 0, NA),
   hour_4 = c(5, 3, 0),
   hour_5 = c(4, 4, 1),
   hour_6 = c(NA, 2, 1),
   hour_7 = c(NA, 1, 1),
   hour_8 = c(NA, 3, NA),
   hour_9 = c(NA, 4, NA),
   hour_10 = c(NA, 5, 3),
   hour_11 = c(NA, 2, 1),
   hour_12 = c(NA, 0, 2),
   hour_13 = c(54, 3, 2),
   hour_14 = c(NA, 2, -1),
   hour_15 = c(NA, 4, -1),
   hour_16 = c(56, 5, 0),
   hour_17 = c(49, NA, 2),
   hour_18 = c(NA, 6, 2),
   hour_19 = c(1, 12, 2),
   hour_20 = c(47, NA, 6),
   hour_21 = c(NA, 7, 5),
   hour_22 = c(NA, 8, 7),
   hour_23 = c(102, 6, 5),
   hour_24 = c(NA, 9, 3))

I am able to calculate the average when NA's are removed using

data$average1 <- apply(data [,c("hour_1", "hour_2", "hour_3", "hour_4", "hour_5", "hour_6", "hour_7", "hour_8", "hour_9", "hour_10", "hour_11", "hour_12", "hour_13", "hour_14", "hour_15", "hour_16", "hour_17", "hour_18", "hour_19", "hour_20", "hour_21", "hour_22", "hour_23", "hour_24")], 1, mean, na.rm = TRUE)

I then tried using na.rm = FALSE to calculate the average where NAs are considered 0

data$average2 <- apply(data [,c("hour_1", "hour_2", "hour_3", "hour_4", "hour_5", "hour_6", "hour_7", "hour_8", "hour_9", "hour_10", "hour_11", "hour_12", "hour_13", "hour_14", "hour_15", "hour_16", "hour_17", "hour_18", "hour_19", "hour_20", "hour_21", "hour_22", "hour_23", "hour_24")], 1, mean, na.rm = FALSE)

This is just producing average values of NA though.

> print(data)
  day hour_1 hour_2 hour_3 hour_4 hour_5 hour_6 hour_7 hour_8 hour_9 hour_10 hour_11
1 Mon      4      4      4      5      4     NA     NA     NA     NA      NA      NA
2 Tue      6      0      0      3      4      2      1      3      4       5       2
3 Wed     NA     NA     NA      0      1      1      1     NA     NA       3       1
  hour_12 hour_13 hour_14 hour_15 hour_16 hour_17 hour_18 hour_19 hour_20 hour_21 hour_22
1      NA      54      NA      NA      56      49      NA       1      47      NA      NA
2       0       3       2       4       5      NA       6      12      NA       7       8
3       2       2      -1      -1       0       2       2       2       6       5       7
  hour_23 hour_24  average1 average2
1     102      NA 30.000000       NA
2       6       9  4.181818       NA
3       5       3  2.157895       NA

Solution

  • Using lotus's recommendation I was able to calculate the average with NAs considered 0.

    rowMeans(replace(data[-1], is.na(data[-1]), 0))