Subject: Switching labels on a factor

From: "Muenchen, Robert A (Bob)" <[email-masked]>
Date: Fri, 15 Dec 2006 15:34:15 -0500

Hi All,

I'm perplexed by the way the unclass function displays a factor whose
labels have been swapped with the relevel function. I realize it won't
affect any results and that the relevel did nothing useful in this
particular case. I'm just doing it to learn ways to manipulate factors.
The display of unclass leaves me feeling that the relevel had failed.

I've checked three books & searched R-help, but found no mention of this
particular issue.  

The program below demonstrates the problem. Is this a bug, or is there a
reason for it to work this way?

Thanks,
Bob

mystring<-
("id,workshop,gender,q1,q2,q3,q4
 1,1,f,1,1,5,1
 2,2,f,2,1,4,1
 3,1,f,2,2,4,3
 4,2,f,3,1, ,3
 5,1,m,4,5,2,4
 6,2,m,5,4,5,5
 7,1,m,5,3,4,4
 8,2,m,4,5,5,9")
mydata<-read.table(textConnection(mystring),
   header=TRUE,sep=",",row.names="id",na.strings="9")
mydata

# Create a gender Releveled variable, gR. 
# Now 1=m, 2=f
mydata$gR <- relevel(mydata$gender, "m")

# Print the data to show that the labels of gR match those of gender.
mydata

# Show that the underlying codes have indeed reversed.
as.numeric(mydata$gender)
as.numeric(mydata$gR)

# Unclass the two variables to see that print order 
# implies that both the codes and labels have
# flipped, cancelling each other out. For gR,
# m appears to be associated with 2, and f with 1
unclass(mydata$gender)
unclass(mydata$gR)

=========================================================
  Bob Muenchen (pronounced Min'-chen), Manager  
  Statistical Consulting Center
  U of TN Office of Information Technology
  200 Stokely Management Center, Knoxville, TN 37996-0520
  Voice: (865) 974-5230  
  FAX:   (865) 974-4810
  Email: [email-masked]
  Web:   http://oit.utk.edu/scc, 
  News:  http://listserv.utk.edu/archives/statnews.html

______________________________________________
[email-masked] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.