One way to reorder a factor is to define a new
factor and specify the order of levels using
the "levels" argument of the factor() function.

The first category specified for the "levels"
argument will be the reference category in
model fits such as with lm().


> mydata <- data.frame(y = c(runif(10), runif(10) + 10), grp = c(rep("A", 10), 
> rep("B", 10)))
> mydata
            y grp
1   0.0808684   A
2   0.2930649   A
3   0.4671063   A
4   0.7815386   A
5   0.5360262   A
6   0.8092338   A
7   0.9965648   A
8   0.3549031   A
9   0.3426956   A
10  0.2988377   A
11 10.6528479   B
12 10.7118101   B
13 10.4484731   B
14 10.9638309   B
15 10.7650812   B
16 10.6355089   B
17 10.7003755   B
18 10.2147930   B
19 10.8901356   B
20 10.6319798   B
> lm(y ~ grp, data = mydata)

Call:
lm(formula = y ~ grp, data = mydata)

Coefficients:
(Intercept)         grpB  
     0.4961      10.1654  

> mydata$grp2 <- factor(mydata$grp, levels = c("B", "A"))
> mydata
            y grp grp2
1   0.0808684   A    A
2   0.2930649   A    A
3   0.4671063   A    A
4   0.7815386   A    A
5   0.5360262   A    A
6   0.8092338   A    A
7   0.9965648   A    A
8   0.3549031   A    A
9   0.3426956   A    A
10  0.2988377   A    A
11 10.6528479   B    B
12 10.7118101   B    B
13 10.4484731   B    B
14 10.9638309   B    B
15 10.7650812   B    B
16 10.6355089   B    B
17 10.7003755   B    B
18 10.2147930   B    B
19 10.8901356   B    B
20 10.6319798   B    B
> lm(y ~ grp2, data = mydata)

Call:
lm(formula = y ~ grp2, data = mydata)

Coefficients:
(Intercept)        grp2A  
      10.66       -10.17  

> 



Steven McKinney

Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre

email: [EMAIL PROTECTED]

tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C. 
V5Z 1L3
Canada




-----Original Message-----
From: [EMAIL PROTECTED] on behalf of John Sorkin
Sent: Thu 5/3/2007 7:10 PM
To: [email protected]
Subject: [R] reodering factor
 
R 2.4.1 
Windows XP

How does one reorder a factor?


I have the following data:
> factor(data$Group)
 [1] ZZ ZT ZT ZZ ZZ ZT ZZ ZZ ZT ZT ZT ZT ZZ ZT ZT ZZ ZT ZZ ZT ZZ ZT ZT ZZ ZZ ZT 
ZZ ZT ZZ ZT ZZ ZZ ZT ZZ ZT
Levels: ZT ZZ

In my regression (i.e. lm(y~data$Group) ZT is taken as the reference category 
and I get an estimate for ZZ. I would like ZZ to be the reference category and 
obtain an estimate for ZT.

Thank,
John

John Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)

Confidentiality Statement:
This email message, including any attachments, is for the so...{{dropped}}

______________________________________________
[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.

______________________________________________
[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.

Reply via email to