我正在尝试根据每小时读数计算 24 小时平均值,其中 NA 不计为除数的一部分,并且我在该计算中成功,但我也尝试根据每小时读数计算 24 小时平均值,其中 NA被视为 0 并包含在除数中。
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))
我可以使用
删除 NA 来计算平均值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)
然后我尝试使用
na.rm = FALSE
来计算 NA 被视为 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)
这只是产生 NA 的平均值。
> 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
使用 Lotus 的推荐,我能够计算 NA 被视为 0 的平均值。
rowMeans(replace(data[-1], is.na(data[-1]), 0))