Learn R Programming

SciencesPo (version 1.3.5)

fillin: Filling in missing values fillin replaces missing values with existing observations from other variable or data frame.

Description

Filling in missing values fillin replaces missing values with existing observations from other variable or data frame.

Usage

fillin(x, y, x.var, y.var = NULL, key)

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 x.
x.var
a character string of the name of the variable in x you want to fill in.
y.var
an optional character string of variable name in y that you would like to use to fill in.
key
a character vector of variable names that are shared by x and y that can be used to join the data frames.

Examples

Run this code
# Create data set with missing values
data1 <- 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
data2 <- data.frame(a = c(1,2,1,2),
                     b = c(3,3,4,4),
                     full = c(100, 200, 300, 400))

# Fill in missings from data1 with values from data2
Filled <- fillin(data1, data2, x.var = "fNA", y.var = "full", key = c("a", "b"))

Run the code above in your browser using DataLab