Spencer Graves spencer.graves at pdf.com
Thu Feb 5 22:13:39 CET 2004

```      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, k1+k1), levels=F12.lvls)
> F.[1:k1] <- F1
> F.[-(1:k1)] <- F2
> proc.time()-start.time
 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
 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:

>
> F1 <- factor(c("b", "a"))
> F2 <- factor(c("c", "b"))
> F3 <- factor(c(levels(F1)[F1], levels(F2)[F2]))
>
> -sundar
>
> Spencer Graves wrote:
>
>>  > F1 <- factor(c("b", "a"))
>>  > F2 <- factor(c("c", "b"))
>>  > k1 <- length(F1)
>>  > k2 <- length(F2)
>>  > F12.lvls <- unique(c(levels(F1), levels(F2)))
>>  > F. <- factor(rep(F12.lvls, k1+k1), levels=F12.lvls)
>>  > F.[1:k1] <- F1
>>  > F.[-(1:k1)] <- F2
>>  > F.
>>  b a c b
>> Levels: a b c
>>
>>      This saves converting the factors to characters, which might
>> save computer time at the expense of your time.      hope this
>> helps.      spencer graves
>>
>> Svetlana Eden wrote:
>>
>>> I have two factors l1, l2, and I'd like to merge them.
>>>
>>> (Remark:       The factors can not be converted to charaters)
>>>
>>> Function c() does not give me the result I want:
>>>
>>>
>>>
>>>
>>>> l1 = factor(c('aaaa', 'bbbb'))
>>>> l2 = factor(c('ccc', 'dd'))
>>>> lMerge = factor(c(l1, l2))
>>>> lMerge
>>>>
>>>
>>>
>>>  1 2 1 2
>>> Levels: 1 2
>>>
>>>
>>> I'd like to merge l1 and l2 and to get lMerge
>>> ----------------------------------------------
>>>
>>>  aaaa bbbb ccc dd
>>> Levels: aaaa bbbb ccc dd
>>>
>>>
>>>  1 2 1 2
>>> Levels: 1 2
>>>
>>>
>>>
>>> How should I do that without converting the factors back to strings.
>>> -------------------------------------------------------------------
>>>
>>>
>>>
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://www.stat.math.ethz.ch/mailman/listinfo/r-help