Examples for 'base::max'


Maxima and Minima

Aliases: max min pmax pmin pmax.int pmin.int

Keywords: univar arith

### ** Examples

require(stats); require(graphics)
 min(5:1, pi) #-> one number
[1] 1
pmin(5:1, pi) #->  5  numbers
[1] 3.141593 3.141593 3.000000 2.000000 1.000000
x <- sort(rnorm(100));  cH <- 1.35
pmin(cH, quantile(x)) # no names
[1] -1.9042348 -0.4821544  0.1982639  0.8211355  1.3500000
pmin(quantile(x), cH) # has names
        0%        25%        50%        75%       100% 
-1.9042348 -0.4821544  0.1982639  0.8211355  1.3500000 
plot(x, pmin(cH, pmax(-cH, x)), type = "b", main =  "Huber's function")
plot of chunk example-base-max-1
cut01 <- function(x) pmax(pmin(x, 1), 0)
curve(      x^2 - 1/4, -1.4, 1.5, col = 2)
curve(cut01(x^2 - 1/4), col = "blue", add = TRUE, n = 500)
plot of chunk example-base-max-1
## pmax(), pmin() preserve attributes of *first* argument
D <- diag(x = (3:1)/4) ; n0 <- numeric()
stopifnot(identical(D,  cut01(D) ),
          identical(n0, cut01(n0)),
          identical(n0, cut01(NULL)),
          identical(n0, pmax(3:1, n0, 2)),
          identical(n0, pmax(n0, 4)))

[Package base version 4.2.3 Index]