Svetlana Eden
2004-Feb-05 20:11 UTC
[R] correction to the previously asked question (about merging factors)
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] 1 2 1 2 Levels: 1 2>I'd like to merge l1 and l2 and to get lMerge ---------------------------------------------- [1] aaaa bbbb ccc dd Levels: aaaa bbbb ccc dd instead of ---------- [1] 1 2 1 2 Levels: 1 2>How should I do that without converting the factors back to strings. ------------------------------------------------------------------- -- Svetlana Eden Biostatistician II School of Medicine Department of Biostatistics Vanderbilt University -- Svetlana Eden Biostatistician II School of Medicine Department of Biostatistics Vanderbilt University
Spencer Graves
2004-Feb-05 20:44 UTC
[R] correction to the previously asked question (about merging factors)
What about the following: > 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[1], k1+k1), levels=F12.lvls) > F.[1:k1] <- F1 > F.[-(1:k1)] <- F2 > F. [1] 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] 1 2 1 2 >Levels: 1 2 > > >I'd like to merge l1 and l2 and to get lMerge >---------------------------------------------- > >[1] aaaa bbbb ccc dd >Levels: aaaa bbbb ccc dd > >instead of >---------- > >[1] 1 2 1 2 >Levels: 1 2 > > > >How should I do that without converting the factors back to strings. >------------------------------------------------------------------- > > >
DJNordlund@aol.com
2004-Feb-05 20:55 UTC
[R] correction to the previously asked question (about merging factors)
>In a message dated 2/5/2004 12:25:01 PM Pacific Standard Time, >svetlana.eden at vanderbilt.edu writes: >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] 1 2 1 2 >Levels: 1 2 > >I'd like to merge l1 and l2 and to get lMerge >---------------------------------------------- > >[1] aaaa bbbb ccc dd >Levels: aaaa bbbb ccc ddHow about something like: IMerge<- factor(c(as.character(I1),as.character(I2))) Dan Nodlund