To find the column mean by excluding NA’s can be easily done by using na,rm but if we want to have NA if all the values are NA then it won’t be that straight forward. Therefore, in such situation, we can use ifelse function and return the output as NA if all the values are NA as shown in the below examples.
Consider the below data frame −
x1<-sample(c(NA,2,3),20,replace=TRUE) x2<-rep(NA,20) df1<-data.frame(x1,x2) df1
x1 x2 1 2 NA 2 NA NA 3 NA NA 4 2 NA 5 2 NA 6 NA NA 7 3 NA 8 NA NA 9 2 NA 10 3 NA 11 2 NA 12 NA NA 13 3 NA 14 2 NA 15 3 NA 16 NA NA 17 2 NA 18 2 NA 19 2 NA 20 2 NA
Finding the mean of columns x1 and x2 and return NA if all values are NA otherwise getting the mean of remaining values −
ifelse(all(is.na(df1$x1)),NA,mean(df1$x1,na.rm=T))
[1] 2.285714
ifelse(all(is.na(df1$x2)),NA,mean(df1$x2,na.rm=T))
[1] NA
y1<-sample(c(NA,rpois(1,5)),20,replace=TRUE) y2<-rep(NA,20) df2<-data.frame(y1,y2) df2
y1 y2 1 8 NA 2 NA NA 3 NA NA 4 NA NA 5 NA NA 6 NA NA 7 8 NA 8 8 NA 9 NA NA 10 NA NA 11 8 NA 12 8 NA 13 8 NA 14 8 NA 15 NA NA 16 NA NA 17 NA NA 18 8 NA 19 NA NA 20 NA NA
Finding the mean of columns y1 and y2 and return NA if all values are NA otherwise getting the mean of remaining values −
ifelse(all(is.na(df2$y1)),NA,mean(df2$y1,na.rm=T))
[1] 8
ifelse(all(is.na(df2$y2)),NA,mean(df2$y2,na.rm=T))
[1] NA