Skip to content
Prev 43771 / 398513 Next

correction to the previously asked question (about merging factors)

Sundar:  Your solution is not only more elegant than mine, it's 
also faster, at least with this tiny example: 

 > start.time <- proc.time()
 > k1 <- length(F1)
 > k2 <- length(F2)
 > F12.lvls <- unique(c(levels(F1), levels(F2)))
 > F. <- factor(rep(F12.lvls[1], k1+k1), levels=F12.lvls)
 > F.[1:k1] <- F1
 > F.[-(1:k1)] <- F2
 > proc.time()-start.time
[1] 0.00 0.00 0.42   NA   NA
 >
 > start.time <- proc.time()
 > F1 <- factor(c("b", "a"))
 > F2 <- factor(c("c", "b"))
 > F3 <- factor(c(levels(F1)[F1], levels(F2)[F2]))
 > proc.time()-start.time
[1] 0.00 0.00 0.24   NA   NA
 >
      With longer vectors, mine may be faster -- but yours is still more 
elegant. 

      Best Wishes,
      spencer graves
Sundar Dorai-Raj wrote: