fill() function

added July 22, 2010

Code File

The code for the fill() function is contained in the file fill.R. You can source the file directly into R by calling

      source("http://www.biostat.jhsph.edu/~pmurakam/fill.R")
    
The code is licensed under the GNU General Public License version 3, or (at your option) any later version.

Manual page

Description:
Sometimes you have a vector of data with missing values and you want to fill in those missing values 
with the nearest non-missing value before or the nearest non-missing value after. This will do that.  
(This was written with data preprocessing, rather than missing value imputation, in mind.)

Usage:
fill(x,direction="before")

Arguments:
x         - data vector (e.g., a column in a matrix)
direction - either "before" or "after". "before" tells the function to fill in missing values 
            with the nearest non-missing value before. "after" tells the function to fill in missing
            values with the nearest non-missing value after.

Example:
vec <- c(NA,NA,1,NA,NA,2,NA,3,NA)
fill(vec,"before")
fill(vec,"after")