Re: [R] Fill in NA values in vector with previous character/factor

From: Christos Hatzis <christos.hatzis_at_nuverabio.com>
Date: Mon, 28 Jul 2008 16:58:07 -0400

Just for the fun of it, here is a recursive solution to the same problem...

rna <- function(z) {

	y <- c(NA, head(z, -1))
	z <- ifelse(is.na(z), y, z)
	if (any(is.na(z))) Recall(z) else z }

> x

 [1] "A" "B" NA NA "C" NA NA NA NA "D" NA NA
> rna(x)

 [1] "A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D"

-Christos

Christos Hatzis, Ph.D.
Nuvera Biosciences, Inc.
400 West Cummings Park
Suite 5350
Woburn, MA 01801
Tel: 781-938-3830
www.nuverabio.com  

> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Bert Gunter
> Sent: Monday, July 28, 2008 4:05 PM
> To: 'Dimitris Rizopoulos'; 'Owen Jones'
> Cc: r-help_at_r-project.org
> Subject: Re: [R] Fill in NA values in vector with previous
> character/factor
>
> Just for the record, a less efficient but certainly simple
> and maybe adequate way for you to do it in standard R (no zoo
> package required) is:
>
> for(i in seq_along(x)[-1])if(is.na(x[i])) x[i] <- x[i-1]
>
> I tried timing it on my not so fancy Windows desktop for a
> vector of 10,000 values, but it was instantaneous.
>
> Loops may be relatively inefficient in R, but how relatively
> inefficient is ... well, ... relative. (If you didn't have
> zoo installed, how long does it take to install? -- perhaps a
> red herring, but ...)
>
> Cheers,
>
> Bert Gunter
> Genentech
>
> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Dimitris Rizopoulos
> Sent: Monday, July 28, 2008 12:26 PM
> To: Owen Jones
> Cc: r-help_at_r-project.org
> Subject: Re: [R] Fill in NA values in vector with previous
> character/factor
>
> try function na.locf() from package 'zoo', i.e.,
>
> library(zoo)
> x <- c("A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA)
> na.locf(x)
>
>
> I hope it helps.
>
> Best,
> Dimitris
>
>
> ----
> Dimitris Rizopoulos
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/(0)16/336899
> Fax: +32/(0)16/337015
> Web: http://med.kuleuven.be/biostat/
> http://perswww.kuleuven.be/dimitris_rizopoulos/
>
>
> Quoting Owen Jones <owen.jones_at_imperial.ac.uk>:
>
> > I have a vector of data (species names) interspersed with NA values
> > and I want a function to "fill in the blanks", replacing NA values
> > with whatever the last species name was.
> >
> > For example the vector:
> >
> > "A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA.
> >
> > should evaluate to:
> >
> > "A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D"
> >
> >
> > I tried to use rle() in a function to do this but have hit
> a brick wall.
> >
> > How would YOU do this?
> >
> > Many thanks,
> >
> > Owen
> >
> > ______________________________________________
> > R-help_at_r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
>
>
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 28 Jul 2008 - 20:54:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Jul 2008 - 21:33:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive