Re: [R] without a loop

From: Spencer Graves <spencer.graves_at_pdf.com>
Date: Wed 08 Jun 2005 - 01:16:56 EST

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:

> tmp <- c(-1,NA,NA,1,1,NA,NA,1)
>
> without using a loop, how can I replace all NAs in the list above with
> the previous none NA value in the list?

>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jun 08 01:29:53 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:27 EST