The difference of na.rm and na.omit in R

后端 未结 2 1405
野的像风
野的像风 2021-02-08 22:55

I\'ve just started with R and I\'ve executed these statements:

library(datasets)
head(airquality)
s <- split(airquality,airquality$Month)
sapply(s, function(x         


        
2条回答
  •  一生所求
    2021-02-08 23:08

    sapply(s, function(x) {colMeans(x[,c("Ozone", "Solar.R", "Wind")], na.rm = TRUE)}) treats each column individually, and calculates the average of the non-NA values in each column.

    lapply(s, function(x) {colMeans(na.omit(x[,c("Ozone", "Solar.R", "Wind")])) }) subsets sto those cases where none of the three columns are NA, and then takes the column means for the resulting data.

    The difference comes from those rows which have one or two of the values as NA.

提交回复
热议问题