Calculate the mean by colum when there is NA

Hi,
How could I ignore NA to calculate the mean of each column, instead of deleting the NA and compose a new data frame calculation. As the result, it seems not right. is it possible to ignore the NA in batch processing way, instad of one by one manually?Thank you in advance!

d1<-data.frame(c1=c(2,NA,3,4,5,6),C2=c(9,2,3,4,5,7),C3=c(3,4,NA,NA,6,2))
View(d1)
apply(d1,2,function(x) mean(x))
#> c1 C2 C3 
#> NA  5 NA

Created on 2022-11-07 with reprex v2.0.2

Add na.rm = TRUE to remove NA's when calculating the mean.

d1<-data.frame(c1=c(2,NA,3,4,5,6),C2=c(9,2,3,4,5,7),C3=c(3,4,NA,NA,6,2))
d1
#>   c1 C2 C3
#> 1  2  9  3
#> 2 NA  2  4
#> 3  3  3 NA
#> 4  4  4 NA
#> 5  5  5  6
#> 6  6  7  2
apply(d1,2,function(x) mean(x, na.rm = TRUE))
#>   c1   C2   C3 
#> 4.00 5.00 3.75

Created on 2022-11-06 with reprex v2.0.2

3 Likes

Alternatively,

colMeans(d1, na.rm = TRUE)

3 Likes

Thank you for your help. It works!

That's a good idea.It also works!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.