[R] pmax and sort?

Greg Snow Greg.Snow at imail.org
Tue Jan 13 02:39:50 CET 2009


Here are a couple of quick examples that may help:

> 1:10
 [1]  1  2  3  4  5  6  7  8  9 10
> 10:1
 [1] 10  9  8  7  6  5  4  3  2  1
> pmax( 10:1, 1:10 )
 [1] 10  9  8  7  6  6  7  8  9 10
> pmin( 1:10, 5 )
 [1] 1 2 3 4 5 5 5 5 5 5

In the first example with pmax, there are 2 vectors being compared (10-1, 1-10), so first 10 is compared to 1 and since 10 is larger it is the first element of the returned vector, then 9 is compared to 2 and 9 becomes the second element, this continues till the 10th comparison is 1 vs. 10 and 10 is the final element.  The second (pmin) example shows the recycling, each of the numbers 1-10 is compared to the number 5, in each case the smaller of the pair is returned (1-4 for the first 4 elements, then 5 for the rest).

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of rkevinburton at charter.net
> Sent: Monday, January 12, 2009 5:55 PM
> To: r-help at r-project.org
> Subject: [R] pmax and sort?
> 
> I am having a hard time understanding the documentation and I was
> wondering if there would be someone to help clear the cobwebs.
> 
> The documentation for pmax states:
> 
> pmax and pmin take one or more vectors (or matrices) as arguments and
> return a single vector giving the ‘parallel’ maxima (or minima) of the
> vectors. The first element of the result is the maximum (minimum) of
> the first elements of all the arguments, the second element of the
> result is the maximum (minimum) of the second elements of all the
> arguments and so on. Shorter inputs are recycled if necessary.
> attributes (such as names or dim) are transferred from the first
> argument (if applicable).
> 
> This descirption seems to me like what sort would return. Would someone
> please help me understand the differenece between pmax and sort?
> 
> Thank you.
> 
> Kevin
> 
> ______________________________________________
> 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.


More information about the R-help mailing list