| mergeFac | R Documentation |
Given a factor of length n that contains some missing values, fill
in the missing values with the corresponding values of others factors.
mergeFac(x, ...)
x |
Factor variable. |
... |
Other factor variables. |
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.
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.
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>
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.