Chris, Argh!!! I was writing a reply just now insisting that the output below makes no sense when it finally hit me: the first line of output from the unclass function is just the data and bears no relationship whatsoever with the order of the "m" and "f" below it. I had gotten the idea that it picked up on the first value and so displayed the label to match. I don't even want to think about how much time I spent working on a "problem" that was nonexistent!
Thank you very much for your help! Bob > unclass(mydata$gR) [1] 2 2 2 2 1 1 1 1 attr(,"levels") [1] "m" "f" ========================================================= 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 PROTECTED] Web: http://oit.utk.edu/scc, News: http://listserv.utk.edu/archives/statnews.html ========================================================= -----Original Message----- From: Chris Andrews [mailto:[EMAIL PROTECTED] Sent: Monday, December 18, 2006 12:05 PM To: Muenchen, Robert A (Bob) Cc: [email protected] Subject: Re: [R] Switching labels on a factor 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)" <[EMAIL PROTECTED]> Subject: [R] Switching labels on a factor To: <[email protected]> Message-ID: <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] 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, [EMAIL PROTECTED], 716 829 2756 ______________________________________________ [email protected] 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.
