Skip to content
Prev 305596 / 398506 Next

multi-column factor

I have a data frame with columns which draw on the same underlying
universe, so I want them to be factors with the same level set:

--8<---------------cut here---------------start------------->8---
'data.frame':	3 obs. of  2 variables:
 $ a: chr  "a" "b" "c"
 $ b: chr  "b" "c" "d"
'data.frame':	3 obs. of  2 variables:
 $ a: Factor w/ 4 levels "a","b","c","d": 1 2 3
 $ b: Factor w/ 4 levels "a","b","c","d": 2 3 4
--8<---------------cut here---------------end--------------->8---
factor(z$a,levels=union(z$a,z$b))
is factor(z$a,levels=union(z$a,z$b)) the right way to handle this?
maybe there is a better way to extract levels than union()?
(bear in mind that I have ~10M rows and ~1M levels, so performance is an
issue).

Thanks!