[R] adding in missing values in a sequence

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Thu Nov 15 00:04:03 CET 2007


On Thu, 15 Nov 2007, Andrew Hoskins wrote:

> Hi,
>
> I have a data frame with two columns of data, one an indexing column
> and the other a data column. My issue is, this data frame is
> incomplete and there are missing lines.  I want to know how I can
> find and add data into these missing lines.  See example below

You could use a "zoo" series (from the "zoo" package) which provides
infrastructure for indexed observations. With your example data:

data <- data.frame(index = c(1:4, 6:10),
  data = c(1.5,4.3,5.6,6.7,7.1,12.5,14.5,16.8,3.4))

you can create a series

z <- zoo(data$data, data$index)

end extend it to the grid 1:10

z <- merge(zoo(,1:10), z)

which then has an NA at index 5. Then you could use linear interpolation
to replace that NA

na.approx(z)

or replace it with some other number

z[is.na(z)] <- 42

See vignette("zoo", package = "zoo") for more details.
Z



More information about the R-help mailing list