[R] Please help with min()

(Ted Harding) Ted.Harding at manchester.ac.uk
Sun Apr 18 01:28:19 CEST 2010


On 17-Apr-10 23:14:32, Mark Na wrote:
> Hello,
> I have two vectors of length = 10
> 
> x<-c(2,14,79,27,3,126,15,1,12,4)
> y<-rep(4,10)
> 
> and I would like to create a third vector of length = 10 that
> contains the smallest value at each position in the two above vectors.
> 
> I have tried:
> 
> z<-min(x,y)
> 
> but that doesn't work.
> 
> With the example data above, the third vector would look like this.
> 
>> z
>  [1]   2  4  4  4   3 4  4   1  4   4
> 
> Any help with this would be much appreciated, thanks!
> 
> mark

What you need is pmin()

>From ?min :
   'max' and 'min' return the maximum or minimum of _all_ the
    values present in their arguments

Note "all": in other words, your min(x,y) is equivalent to

  min(c(2,14,79,27,3,126,15,1,12,4,4,4,4,4,4,4,4,4,4,4))

(which = 1).

Further from ?min :

  '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.

Hence pmin() ("p" for "parallel").

Ted.




--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 18-Apr-10                                       Time: 00:28:16
------------------------------ XFMail ------------------------------



More information about the R-help mailing list