Bob,
This is I think exactly what one wants to have happen. The first four
observations are still women. Both the labels and the underlying integers
should change. (If you want to give all the people sex changes, try Relevel in
the Epi package.
mydata$afterthechange <- Relevel(mydata$gender, list(m="f",
f="m"))
mydata
workshop gender q1 q2 q3 q4 gR afterthechange
1 1 f 1 1 5 1 f m
2 2 f 2 1 4 1 f m
3 1 f 2 2 4 3 f m
4 2 f 3 1 NA 3 f m
5 1 m 4 5 2 4 m f
6 2 m 5 4 5 5 m f
7 1 m 5 3 4 4 m f
8 2 m 4 5 5 NA m f
unclass(mydata$afterthechange)
[1] 1 1 1 1 2 2 2 2
attr(,"levels")
[1] "m" "f"
Chris
Date: Fri, 15 Dec 2006 15:34:15 -0500
From: "Muenchen, Robert A (Bob)" <muenchen at utk.edu>
Subject: [R] Switching labels on a factor
To: <R-help at stat.math.ethz.ch>
Message-ID:
<7270AEC73132194E8BC0EE06B35D93D84A061C at UTKFSVS3.utk.tennessee.edu>
Content-Type: text/plain; charset="US-ASCII"
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: muenchen at utk.edu
Web: http://oit.utk.edu/scc,
News: http://listserv.utk.edu/archives/statnews.html
--
Christopher Andrews, PhD
SUNY Buffalo, Department of Biostatistics
242 Farber Hall, candrews at buffalo.edu, 716 829 2756