Delete comment from: One R Tip A Day
rtwillia said...
Hi Paolo,
I have a question that I can't seem to find a good answer to. I frequently have missing data that need to be replaced by values of another variable in the data set. For example:
d<-data.frame(x=c(1,2,3,4,5),y=c(NA,NA,6,7,8))
Where y=NA, I want it to assume the value of x[i]. The only thing that I've found that works is a loop, but it's enormously sluggish on large data frames. This is what I've used in the past:
for(i in 1:nrow(d)){
d[[i,2]][is.na(d[[i,2]])]=d[[i,1]]
}
Do you know of a more efficient solution to this?
Thanks!
Sep 23, 2011, 10:27:07 PM

