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

Michael Lachmann lachmann at eva.mpg.de
Sat Sep 1 21:11:43 CEST 2007


This message didn't seem to have been somewhat forgotten. Here is a reply.

When you constructed the data.frame, all strings were converted to
factors. If you didn't want that, it would have been possible to
specify it:

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

Then everything would work as intended:

one.line<-as.character(df[df$a=="Abraham",])

Actually, the real problem is that df[df$a=="Abraham",] returns a
list. There is no need to use as.character here, just unlist:

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

The returned list is also the problem with your code.

If you have a data.frame with factors, then

df[df$a=="Abraham",]

returns a list. Each element of this list is a factor, and has a
different set of levels. Thus, look at the following output:
 > c(df[df$a=="Abraham",])
$a
[1] Abraham
Levels: Abraham Jonah Moses

$b
[1] Sarah
Levels: Hannah Mary Sarah

$c
[1] Billy
Levels: Billy Bob Joe

It is quite obvious why it is so complicated to untangle these. I
think the best way would be:

one.line<- sapply(df[df$a=="Abraham",],as.character)

Michael

-----Original Message-----
From: r-help-bounces_at_stat.math.ethz.ch
[mailto: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
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 work!



More information about the R-help mailing list