[R] returning quartiles of a list?

Greg Trafton trafton at itd.nrl.navy.mil
Tue Jul 10 17:10:28 CEST 2001


>>>>> Thomas Lumley writes:

>> Hi, all.  I have a list:
>> 
>> process <- c( 5 , 7 , 4 , 1 , 4 , 1)
>> 
>> and I'd like to get each half (or each third or each quartile) of
>> the list:
>> 

> n<-length(process)
> process[1:(n/2)]
> process[(n/2):1]
> process[(n/4):(n/2)]
> etc, etc,

Hmmm...  I tried this first and it only works for even lists

> process <- c( 5 , 7 , 4 , 1 , 4 , 1)
> n<-length(process)
> n
[1] 6
> process[1:(n/2)]
[1] 5 7 4

(works fine)

> process[(n/2):1]
[1] 4 7 5

(this is just a reverse of the previous)

> process[(n/2):n]
[1] 4 1 4 1

(this double counts the first 4, of course)

> process[((n/2)+1):n]
[1] 1 4 1

(but this works for even lists)

> process[(n/4):(n/2)]
[1] 5 7

(works)

> process <- c( 5 , 7 , 4 , 1 , 4 , 1, 8)

(and this is now an odd list)

> process[1:(n/2)]
[1] 5 7 4

(works fine)

> process[((n/2)+1):n]
[1] 1 4 1

(but the bit that worked for the even lists above doesn't work here
(and I don't quite understand why not...)

thanks!
greg
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list