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

Christos Hatzis christos.hatzis at nuverabio.com
Mon Jul 28 22:58:07 CEST 2008


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.
> 
>



More information about the R-help mailing list