Re: [R] Re-order levels of a categorical (factor) variable

2015-01-22 Thread Ravi Varadhan
Thursday, January 22, 2015 10:29 AM To: Bert Gunter Cc: William Dunlap; r-help@r-project.org; Ravi Varadhan Subject: Re: [R] Re-order levels of a categorical (factor) variable >>>>> Bert Gunter >>>>> on Wed, 21 Jan 2015 18:52:12 -0800 writes: > Bill/Rav

Re: [R] Re-order levels of a categorical (factor) variable

2015-01-22 Thread Martin Maechler
> Bert Gunter > on Wed, 21 Jan 2015 18:52:12 -0800 writes: > Bill/Ravi: > I believe the problem is that the factor is automatically created when > a data frame is created by read.table(). By default, the levels are > lexicographically ordered. The following reproduces

Re: [R] Re-order levels of a categorical (factor) variable

2015-01-21 Thread Bert Gunter
Bill/Ravi: I believe the problem is that the factor is automatically created when a data frame is created by read.table(). By default, the levels are lexicographically ordered. The following reproduces the problem and gives a solution. >library(lattice) > z <- data.frame(y = 1:9, x = rep(c("pre"

Re: [R] Re-order levels of a categorical (factor) variable

2015-01-21 Thread William Dunlap
Are you sure the factors of T are in the order you think they are? (Are you sure you are using the expected version of T.) Use print(levels(T)) to make sure. I tried timeCats <- c("Presurgery", "Day 30", "Day 60", "Day 180", "Day 365") d <- data.frame(T = factor(rep(timeCats, 11:15), lev