[R] How to calculate row means while ignore NAs
lily li
chocold12 at gmail.com
Fri Oct 28 20:06:25 CEST 2016
Hi R users,
I have the dataframe as below:
w=c(5,6,7,8)
x=c(1,2,3,4)
y=c(1,2,3)
length(y)=4
z=data.frame(w,x,y)
z$mean1 <- rowMeans(subset(z, select = c(x, y)), na.rm = T)
z$mean2 <- rowMeans(subset(z, select = c(x, y)), na.rm=F)
w x y mean1 mean2
1 5 1 1 1 1
2 6 2 2 2 2
3 7 3 3 3 3
4 8 4 NA 4 NA
How to calculate mean value for the three columns, while default removing
NAs.
For example, for the fourth row, the mean value should be (8+4)/2 = 6
Thanks for your help.
[[alternative HTML version deleted]]
More information about the R-help
mailing list