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)
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@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]]
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
hi Jorge,> * Jorge I Velez <wbetrvinairyrm at tznvy.pbz> [2012-10-18 16:43:58 +1100]: > >> 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 ...is sort(unique()) really necessary? I think lev <- levels(a) should be enough. However, this does not quite do what I want. I want a function which will _NOT_ have a non-factor vector as an intermediate value because that would waste a LOT of memory in my case. I want a function which will check that a and b have identical levels (in Lisp lingo, the levels are EQ, not just EQUALP). --8<---------------cut here---------------start------------->8---> a <- factor(letters[sample(1:10,20,replace=TRUE)],levels=letters)[1] e e a b c e j d a b h i a e e g j a c e Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z> b <- factor(letters[sample(1:10,30,replace=TRUE)],levels=letters)[1] d d f c j b d e j j g i g j j g g a j a b e d c b i i a b f Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z> c(a,b)[1] 5 5 1 2 3 5 10 4 1 2 8 9 1 5 5 7 10 1 3 5 4 4 6 3 10 [26] 2 4 5 10 10 7 9 7 10 10 7 7 1 10 1 2 5 4 3 2 9 9 1 2 6> factor(letters[c(a,b)],levels=letters)[1] e e a b c e j d a b h i a e e g j a c e d d f c j b d e j j g i g j j g g a [39] j a b e d c b i i a b f Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z --8<---------------cut here---------------end--------------->8--- however, this is not a "direct" way (unlike my unlist(list(...))): there is an intermediate integer vector c(a,b) which is mapped to a character vector via letters, which is converted back to integers (==factors). IIUC, a factor is an integer vector which knows that the integers refer to levels. c(a,b) creates such an integer vector. How do I tell it that it is a factor? -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://www.memritv.org http://www.PetitionOnline.com/tap12009/ http://dhimmi.com usually: can't pay ==> don't buy. software: can't buy ==> don't pay