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

Andrew Yee yee at post.harvard.edu
Tue Jul 10 17:56:41 CEST 2007


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