Skip to content

What is behind class coercion of a factor into a character

3 messages · Tal Galili, Bert Gunter, Rolf Turner

#
Tal:

There was a recent discussion on this list about this (Sam Steingold
was the OP IIRC).

The issue is ?c . In particular:

"c is sometimes used for its side effect of removing attributes except
names, for example to turn an array into a vector."

Hence, the factor attribute is removed and you get what you saw. As
regards it's "rationale," you may find Bill Dunlap's comments on
"c()'s unfortunate history" relevant. The problem with factors is
"what should concatenation do, anyway?" If a <- factor(c("x", "y"))
and b <- factor(c("y", "z")), what should c(a,b) be? -- There is no
reason to assume that the "y" in a is the same as the "y" in b!

Cheers,
Bert
On Mon, Oct 22, 2012 at 6:46 AM, Tal Galili <tal.galili at gmail.com> wrote:

  
    
#
WARNING:  Use with caution!

There is a way to effect the catenation of factors:  The data.frame
method for rbind() does this.  E.g.

set.seed(42)
f1 <- factor(sample(letters[1:3],42,TRUE))
f2 <- factor(sample(letters[1:4],66,TRUE))
d1 <- data.frame(f=f1)
d2 <- data.frame(f=f2)
dd <- rbind(d1,d2)
ff   <- dd[,1]

et voila, ff is the "desired" catenation of f1 and f2.
But heed Bert's words of caution below!

     cheers,

         Rolf Turner
On 23/10/12 02:58, Bert Gunter wrote: