[R] Can R fill in missing values?

Erik Iverson iverson at biostat.wisc.edu
Thu Jul 24 05:34:39 CEST 2008


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



More information about the R-help mailing list