# [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

```Andrew:

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

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)
mx[mx[,"a"]=="Abraham",,drop=TRUE]

HTH.

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
ofcharacters?

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
suggestions?

See the following code:

a<-c("Abraham","Jonah","Moses")
b<-c("Sarah","Hannah","Mary")
c<-c("Billy","Joe","Bob")

df<-data.frame(a=a,b=b,c=c)

#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

#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
work!

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help