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

From: Bert Gunter <gunter.berton_at_gene.com>
Date: Mon, 28 Jul 2008 13:05:26 -0700

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. Received on Mon 28 Jul 2008 - 20:10:35 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