[R] how to concatenate factor vectors?

arun smartpink111 at yahoo.com
Thu Oct 18 14:33:33 CEST 2012


Hi,
May be this also works:
 a <- factor(c(1,3,5))
 b <- factor(c(5,7))
f1<-as.numeric(c(as.character(a),as.character(b)))
lev<-as.numeric(c(levels(a),setdiff(levels(b),levels(a))))
f2<-factor(f1,levels=lev)
 f2
#[1] 1 3 5 5 7
#Levels: 1 3 5 7

a1<-factor(5:1,levels=1:9)
 b1<-factor(9:1,levels=1:9)
f<-as.numeric(c(as.character(a1),as.character(b1)))
 lev1<-as.numeric(c(levels(a1),setdiff(levels(b1),levels(a1))))
 f3<-factor(f,levels=lev1)
 f3
# [1] 5 4 3 2 1 9 8 7 6 5 4 3 2 1
#Levels: 1 2 3 4 5 6 7 8 9

A.K.




----- Original Message -----
From: Bert Gunter <gunter.berton at gene.com>
To: Jorge I Velez <jorgeivanvelez at gmail.com>
Cc: r-help at r-project.org; sds at gnu.org
Sent: Thursday, October 18, 2012 2:21 AM
Subject: Re: [R] how to concatenate factor vectors?

No. You need to test more carefully.

> a <- factor(c(1,3,5))
> b <- factor(c(5,7))
> c(a,b)
[1] 1 2 3 1 2
> lev <- sort(unique(f <- c(a,b)))
> f <- factor(f,levels=lev)
> f
[1] 1 2 3 1 2
Levels: 1 2 3

## but

> unlist(list(a,b),use.names=FALSE)
[1] 1 3 5 5 7
Levels: 1 3 5 7

However, Is level "5" in 'a' the same as level "5" in 'b' ? The OP
fails to specify, and there's no reason to assume so.  So I would say
clarification is required before any answer can be given.

-- Bert

On Wed, Oct 17, 2012 at 10:43 PM, Jorge I Velez
<jorgeivanvelez at gmail.com> wrote:
> Hi Sam,
>
> Perhaps the following?
>
>> a <- factor(5:1,levels=1:9)
>> b <- factor(9:1,levels=1:9)
>> lev <- sort(unique(f <- c(a, b)))
>> f <- factor(f, levels = lev)
>> str(f)
>  Factor w/ 9 levels "1","2","3","4",..: 5 4 3 2 1 9 8 7 6 5 ...
>
> HTH,
> Jorge.-
>
>
> On Thu, Oct 18, 2012 at 3:44 PM, Sam Steingold <> wrote:
>
>> How do I concatenate two vectors of factors?
>> --8<---------------cut here---------------start------------->8---
>> > a <- factor(5:1,levels=1:9)
>> > b <- factor(9:1,levels=1:9)
>> > str(c(a,b))
>>  int [1:14] 5 4 3 2 1 9 8 7 6 5 ...
>> > str(unlist(list(a,b),use.names=FALSE))
>>  Factor w/ 9 levels "1","2","3","4",..: 5 4 3 2 1 9 8 7 6 5 ...
>> --8<---------------cut here---------------end--------------->8---
>> so, unlist(list()) works.
>> is there a better way or is this how this is supposed to be done?
>> Thanks!
>> --
>> Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X
>> 11.0.11103000
>> http://www.childpsy.net/ http://honestreporting.com
>> http://think-israel.org http://thereligionofpeace.com
>> http://mideasttruth.com
>> (lisp programmers do it better)
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list