Hello, When making a graph, plot and boxplot automatically sort my factor levels (y axis) into alphabetical order. Is there a way to make it NOT do this? I've tried: sort.by="none", sorted=FALSE, sort=FALSE, reorder=FALSE. Thank you.
The short answer is 'they don't'. They use the order of the levels of the factor that you supply, and the answer is for you to create the factor with the levels in the order you want -- otherwise factor creation uses alphabetical order. See ?factor. On Thu, 1 May 2008, Lydia N. Slobodian wrote:> Hello, > > When making a graph, plot and boxplot automatically sort my factor > levels (y axis) into alphabetical order. Is there a way to make it > NOT do this? I've tried: > > sort.by="none", sorted=FALSE, sort=FALSE, reorder=FALSE. > > Thank you. > > ______________________________________________ > R-help at 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hello. I'm trying find the ratios between each of the integers in a vector. I have: for (n in x) { ratio <- (x[n]/x[n-1]) ratio.all <- c(ratio.all, ratio) } Of course this doesn't work, nor does diff(n)/diff(n-1). Is there a way to specify a pair of integers in a vector? Thank you, Lydia
Hi Lydia, I compared my ratio function with Dimitris and Phil's suggestions. Please do NOT use my approach because it's painfully slow for a large vector (as Phil told me). Here is why (using Win XP SP2, Intel Core- 2 Duo 2.4 GHz, R 2.7.0 Patched): # Vector x=rnorm(100000,0,1) # Suggestion new.ratio=function(x) x[2:length(x)]/x[1:(length(x)-1)] # My horrible function my.ratio=function(x){ temp=NULL for (n in 1:length(x)) temp=c(temp,x[n]/x[n-1]) temp } # System time t=system.time(my.ratio(x)) tnr=system.time(new.ratio(x)) t user system elapsed 38.79 0.06 39.31 tnr user system elapsed 0 0 0 Thanks to all, Jorge On Mon, May 12, 2008 at 11:15 AM, Phil Spector <spector@stat.berkeley.edu> wrote:> Another alternative would be to take advantage of R's vectorization: > > x=c(1,2,3,2,1,2,3) > > x[2:length(x)]/x[1:(length(x)-1)] > > > [1] 2.0000000 1.5000000 0.6666667 0.5000000 2.0000000 1.5000000 > > The solution using your ratio function will be painfully slow > for a large vector. > > - Phil Spector > Statistical Computing Facility > Department of Statistics > UC Berkeley > spector@stat.berkeley.edu > >[[alternative HTML version deleted]]