Re: [R] Can R fill in missing values?

From: Erik Iverson <iverson_at_biostat.wisc.edu>
Date: Wed, 23 Jul 2008 22:34:39 -0500

Hello -

Jia Ying Mei wrote:
> Hi,
>
> I know this can be done in Stata (which is quite messy) but I wanted to
> know if it can be done in R. So lets say I have a merged data set (I
> used the merge function by date for the attached two files), where all
> the missing values are filled with NAs (which is what the all.x=TRUE does).
>
> Is there any way to replace those NAs with the value of the latest row
> that contains a value?
>
> For example:
>
> > Date<-read.table("Desktop/R/Testdate.txt", head=T, sep="\t")
> > Data<-read.table("Desktop/R/Testinput.txt", head=T, sep="\t")
> > Merged<-merge(Date, Data, all.x=TRUE)
> > Merged
> Date France Germany
> 1 3/10/07 2 4
> 2 3/11/07 NA NA
> 3 3/12/07 NA NA
> 4 3/13/07 NA NA
> 5 3/14/07 NA NA
> 6 3/15/07 1 2
>
> Given this Merged data, is there a way to replace every NA value from
> 3/11 to 3/14 with that of 3/15? But then say there are multiple
> intervals with NAs that I want to fill with the last given value?
>

Yes, no loop necessary.

# begin R code
install.packages("zoo")
library(zoo)

Merged$France <- na.locf(Merged$France, fromLast = TRUE)

# end R code

This will of course work on the 'France' column. Use of lapply in conjuction with this idea will lead to solving this problem for N columns in a couple lines of R. Not messy at all!

Best Regards,
Erik Iverson



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 Thu 24 Jul 2008 - 03:38:08 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 Thu 24 Jul 2008 - 04:32:23 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