Reputation: 1359
I have the following data-frame:
i1<-c(5,4,3,4,5)
i2<-c(4,NA,NA,5,3)
i3<-c(NA,4,4,4,5)
i4<-c(3,5,5,NA,2)
dat<-as.data.frame(cbind(i1,i2,i3,i4))
dat
i1 i2 i3 i4
1 5 4 NA 3
2 4 NA 4 5
3 3 NA 4 5
4 4 5 4 NA
5 5 3 5 2
My goal is to replace the NA
with the row mean to get:
> dat
i1 i2 i3 i4
1 5 4.0000 4 3.0000
2 4 4.3333 4 5.0000
3 3 4.0000 4 5.0000
4 4 5.0000 4 4.3333
5 5 3.0000 5 2.0000
I currently have the following code:
dat1<- which(is.na(dat), arr.ind=TRUE)
dat[dat1] <- rowMeans(dat, na.rm=TRUE)[dat1[,1]]
Which does yield the desired result; however, I was just wondering if there is a better way to do this, and also to keep both row and column names (rows will have names in the final product). Thank you.
Upvotes: 2
Views: 868
Reputation: 886938
Try
(is.na(dat))*rowMeans(dat, na.rm=TRUE)[row(dat)] + replace(dat, is.na(dat), 0)
# i1 i2 i3 i4
#1 5 4.000000 4 3.000000
#2 4 4.333333 4 5.000000
#3 3 4.000000 4 5.000000
#4 4 5.000000 4 4.333333
#5 5 3.000000 5 2.000000
Upvotes: 3