mergeFac: Fill in missing values of one factor with corresponding...

View source: R/mergeFac.R

mergeFacR Documentation

Fill in missing values of one factor with corresponding values from another.

Description

Given a factor of length n that contains some missing values, fill in the missing values with the corresponding values of others factors.

Usage

mergeFac(x, ...)

Arguments

x

Factor variable.

...

Other factor variables.

Details

x and the factors named in otherFactors must all have the same length. If they do, missing values in x will be filled in with the corresponding values of the first factor in otherFactors. If the corresponding values of that factor are also missing, mergeFac() will look to the corresponding values of the next factor, and so\NBon.

Note

mergeFac() is deprecated. You should use dplyr::coalesce() instead. No problems with mergeFac() have been reported, but dplyr::coalesce() does all that mergeFac() does, is more powerful, and will be just as well maintained.

Merging factors as mergeFac() does is trickier than just using a command like fac1[is.na(fac1)] <- fac2[is.na(fac1)] because fac1 and fac2 may have different factor levels. This command takes care of the problem by merging the levels among different factors.

Examples

fac1 <- factor(c("a", NA,  "b", NA,  NA))
fac2 <- factor(c("y", "y", "y", NA,  NA))
fac3 <- factor(c(NA,  "z", "z", "z", NA))
mergeFac(fac1, fac2)        # [1] a    y    b    <NA> <NA>
mergeFac(fac1, fac2, fac3)  # [1] a    y    b    z    <NA>

jbullock35/Bullock documentation built on April 1, 2022, 6:21 p.m.