Dear list,
I'm having second thoughts after solving a very trivial problem: I
want to extend the relevel() function to reorder an arbitrary number
of levels of a factor in one go. I could not find a trivial way of
using the code obtained by getS3method("relevel","factor"). Instead, I
thought of solving the problem in a recursive manner (possibly after
reading Paul Graham essays on Lisp too recently). Here is my attempt :
order.factor <- function (x, ref)
{
last.index <- length(ref) # convenience for matlab's end keyword
if(last.index == 1) return(relevel(x, ref)) # end case, normal case
of relevel
my.new.list <- list(x=relevel(x, ref[last.index]), # creating a
list with updated parameters,
# going through the list in reverse order
ref=ref[-last.index]) #
chop the vector from its last level
return(do.call(order.factor, my.new.list)) # recursive call
}
ff <- factor(c("a", "b", "c", "d"))
ff
relevel(ff, levels(ff)[1])
relevel(ff, levels(ff)[2]) # that's the usual case: you want to put
a level first
order.factor(x=ff, ref=c("a", "b"))
order.factor(x=ff, ref=c("c"))
order.factor(x=ff, ref=c("c", "d")) # that's my wish: put c and d in
that order as the first two levels
I'm hoping this can be improved in several aspects:
- there is probably already a better function I missed or overlooked
(I'd still be curious about the following points, though)
- after reading a few threads, it appears that some recursive
functions are fragile in some sense, and I'm not sure what this means
in practice. (Should I use Recall, somehow?)
- it's probably quite slow for large data.frames
- I could not think of a good name, this one might clash with some S3
method perhaps?
- any other thoughts welcome!
Best wishes,
Baptiste
_____________________________
Baptiste AuguiƩ
School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
Phone: +44 1392 264187
http://newton.ex.ac.uk/research/emag
______________________________________________
R-help@r-project.org 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.