+5 votes
in Programming Languages by (73.8k points)
Using two vectors, I want to generate a third vector that should have pairwise maximum values from two vectors. Is there any R function to find the pairwise minimum and maximum from two vectors?

E.g.

 a = c(10,1,2,31,41,14,15)

 b = c(11,0,5,32,40,12,19)

Result for max = 11  1  5 32 41 14 19

1 Answer

+2 votes
by (348k points)
selected by
 
Best answer

You can use the pmin() function to find the pairwise minimum value in two vectors.

To find the pairwise maximum, you can use the pmax() function.

Example:

> a = c(10,1,2,31,41,14,15)
> b = c(11,0,5,32,40,12,19)
> pmin(a,b)
[1] 10  0  2 31 40 12 15
> pmax(a,b)
[1] 11  1  5 32 41 14 19


...