[R] why doesn't as.character of this factor create a vector ofcharacters?

Bert Gunter gunter.berton at gene.com
Tue Jul 10 19:53:44 CEST 2007


As you haven't received a reply yet ...

?factor,?UseMethod, and "An Introduction to R" may help. But it's a bit

Factors are objects that are integer vectors (codes) with a levels attribute
that associates the codes with levels as character names. So
df[df$a=="Abraham",] is a data.frame in which the columns are still factors.
as.character() is a S3 generic function that calls the (internal) default
method on a data.frame. This obviously just turns the vector of integers
into characters and ignores the levels attribute.

t() is also a S3 generic with a data.frame method. This merely converts the
data.frame to a matrix via as.matrix and then applies t() to the matrix. The
as.matrix() method for data.frames captures the levels and converts the
data.frame to a character matrix with the level names, not their numeric
codes.So another perhaps more intuitive but also more storage intensive way
(I think) of doing what you wantthat avoids the transpose and as.vector()
conversion would be:

mx <- as.matrix(df)


Bert Gunter
Genentech Nonclinical Statistics

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Andrew Yee
Sent: Tuesday, July 10, 2007 8:57 AM
To: r-help at stat.math.ethz.ch
Subject: [R] why doesn't as.character of this factor create a vector

I'm trying to figure out why when I use as.character() on one row of a
data.frame, I get factor numbers instead of a character vector.  Any

See the following code:



#Suppose I'm interested in one line of this data frame but as a vector

one.line <- df[df$a=="Abraham",]

#However the following illustrates the problem I'm having

one.line <- as.vector(df[df$a=="Abraham",]) #Creates a one row
data.frame instead of a vector!

#compare above to

one.line <- as.character(df[df$a=="Abraham",]) #Creates a vector of 1, 3, 1!

#In the end, this creates the output that I'd like:

one.line <-as.vector(t(df[df$a=="Abraham",])) #but it seems like a lot of

R-help at stat.math.ethz.ch mailing list
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