[R] how to not sort factors when plotting

Dimitris Rizopoulos Dimitris.Rizopoulos at med.kuleuven.be
Mon May 12 17:14:47 CEST 2008


this can be more efficiently coded using indexing, e.g.,

x <- c(1,2,3,2,1,2,3)

n <- length(x)
x[2:n] / x[1:(n-1)]


Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
      http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting Jorge Ivan Velez <jorgeivanvelez at gmail.com>:

> Hi Lydia,
>
> Try this:
>
> # Function
> ratio=function(x){
> temp=NULL
> for (n in 1:length(x)) temp=c(temp,x[n]/x[n-1])
> temp
> }
>
> # Example
> x=c(1,2,3,2,1,2,3)
>
> ratio(x)
> [1] 2.0000000 1.5000000 0.6666667 0.5000000 2.0000000 1.5000000
>
>
> HTH,
>
> Jorge
>
>
> On Mon, May 12, 2008 at 9:52 AM, Lydia N. Slobodian <lns at cantab.net> wrote:
>
>> 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
>>
>> ______________________________________________
>> 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.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
>



Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list