Skip to content

correction to the previously asked question (about mergin g factors)

1 message · Liaw, Andy

#
First of all, I do not understand why conversion to characters are not
allowed.  That's what Sundar's solution is doing implicitly (but more
elegantly).

Here's a test of all three.  See the function definitions below.
[1] 4.54 0.00 4.73   NA   NA
[1] 3.95 0.01 4.11   NA   NA
[1] 3.61 0.00 3.76   NA   NA
[1] TRUE
[1] TRUE

First, my attempt at generalizing Spencer's suggestion:

mergeFac <- function(...) {
  l <- list(...)
  len <- sapply(l, length)
  lev <- unique(unlist(lapply(l, levels)))
  ans <- factor(rep(lev[1], sum(len)), levels=lev)
  idx.end <- cumsum(len)
  idx.start <- c(1, idx.end[-length(len)] + 1)
  for (i in seq(along=l)) {
    ans[idx.start[i]:idx.end[i]] <- l[[i]]
  }
  ans
}

Then explicit coercion to characters:

mergeFac2 <- function(...) {
  l <- list(...)
  factor(unlist(lapply(l, as.character)))
}

Then Sundar's solution:
mergeFac3 <- function(...) {
  l <- list(...)
  factor(do.call("c", lapply(l, function(x) levels(x)[x])))
}

Cheers,
Andy
------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}