Learn R Programming

SciencesPo (version 1.02.12)

fillNA: Filling in missing values

fillNA uses values of a variable from one data set to fill in missing values in another.

Description

Filling in missing values

fillNA uses values of a variable from one data set to fill in missing values in another.

Usage

fillNA(x, y, xvar, yvar = NULL, KeyVar)

Arguments

x
the data frame with the variable you would like to fill in.
y
the data frame with the variable you would like to use to fill in D1.
xvar
a character string of the name of the variable in D1 you want to fill in.
yvar
an optional character string of variable name in D2 that you would like to use to fill in.
KeyVar
a character vector of variable names that are shared by D1 and D2 that can be used to join the data frames.

Examples

Run this code
# Create data set with missing values
naDF <- data.frame(a = sample(c(1,2), 100, rep=TRUE),
                   b = sample(c(3,4), 100, rep=TRUE),
                   fNA = sample(c(100, 200, 300, 400, NA), 100, rep=TRUE))

# Created full data set
fillDF <- data.frame(a = c(1,2,1,2),
                     b = c(3,3,4,4),
                     fFull = c(100, 200, 300, 400))

# Fill in missing f's from naDF with values from fillDF
Filled <- fillNA(naDF, fillDF, xvar = "fNA", yvar = "fFull", KeyVar = c("a", "b"))

Run the code above in your browser using DataLab