Skip to content
Prev 71290 / 398500 Next

without a loop

How about the following:

tmp <- c(-1,NA,NA,1,1,NA,NA,1)

replaceNA <- function(x){
	iNA <- which(is.na(x))
	if(length(iNA) %in% c(0, length(x)))
		return(x)
	iNA1 <- iNA-1
	iNA1[iNA==0] <- length(x)
	x[iNA] <- x[iNA1]
	replaceNA(x)
}
 > replaceNA(tmp)
[1] -1 -1 -1  1  1  1  1  1
 > replaceNA(NA)
[1] NA

	  spencer graves
Omar Lakkis wrote: