Issue with NA values in R

前端 未结 3 854
时光取名叫无心
时光取名叫无心 2021-01-22 07:44

I feel this should be something easy, I have looked x the internet, but I keep getting error messages. I have done plenty of analytics in the past but am new to R and programmin

3条回答
  •  一生所求
    2021-01-22 08:00

    You can pass the parameter na.rm to your function:

    columnmean <- function(y, na.rm = FALSE){
      nc <- ncol(y)
      means <- numeric(nc)
      for(i in 1:nc) {
        means[i] <- mean(y[,i], na.rm = na.rm)
      }
      means 
    }
    
    data("airquality")
    columnmean(airquality, na.rm = TRUE)
    #[1] 42.129310 185.931507   9.957516  77.882353   6.993464  15.803922
    
    columnmean(airquality)
    #[1]        NA        NA  9.957516 77.882353  6.993464 15.803922
    

    But my recommendation is to look for an alternate code to loops:

    column_mean <- function(y, na.rm = FALSE) {
      sapply(y, function(x) mean(x, na.rm = na.rm))
    }
    
    column_mean(airquality, na.rm = TRUE)
    #     Ozone    Solar.R       Wind       Temp      Month        Day 
    # 42.129310 185.931507   9.957516  77.882353   6.993464  15.803922
    

提交回复
热议问题