max(x,na.rm=FALSE) min(x,na.rm=FALSE)
•
•
> x <- c(1,2.3,2,3,4,8,12,43,-4,-1) > max(x)
[1] 43
> min(x)
[1] -4
Missing value affect the results:
> y<- c(x,NA) > y
[1] 1.0 2.3 2.0 3.0 4.0 8.0 12.0 43.0 -4.0 -1.0 NA
> max(y)
[1] NA
> min(y)
[1] NA
After define na.rm=TRUE, result is meaningful:
> max(y,na.rm=TRUE)
[1] 43
Compare more than 1 vectors:
> x2 <- c(-100,-43,0,3,1,-3) > min(x,x2)
[1] -100
apply() function can be used to calculate the max/min of each row and column of a matrix:
> A <- matrix(c(3,5,7,1,9,4),nrow=3,ncol=2, dimnames=list(c("a","b","c"),c("x","y"))) > Ax y a 3 1 b 5 9 c 7 4 > apply(A, 1, max)#1 is for row a b c 3 9 7 > apply(A, 2, max)#2 is for column x y 7 9 > max.col(A)[1] 1 2 1