YouTip LogoYouTip

R Func Min Max

# R min() and max() Functions - Calculate Minimum and Maximum Values [![Image 3: R Language Examples](#) R Language Examples](#) The R min() and max() functions are used to calculate the minimum and maximum values of elements in a vector, respectively. These two functions are very commonly used in data exploration and outlier detection. The syntax format of the min() and max() functions is as follows: min(x, na.rm = FALSE) max(x, na.rm = FALSE) **Parameter Description:** * **x** Input vector. * **na.rm** Boolean value, defaults to FALSE, sets whether to remove missing values NA. ## Example # Create a vector x <-c(12, 27, 3, 4.2, 2, 2, 54, -21, 4, -2) # Calculate the minimum result.min<-min(x) print(result.min) # Calculate the maximum result.max<-max(x) print(result.max) Executing the above code outputs the following result: -21 54 If you want to find the maximum or minimum value across multiple vectors, you can use pmax() and pmin() for element-wise comparison: ## Example # Two vectors a <-c(10, 20, 30) b <-c(5, 25, 35) # Find the maximum of all elements print(max(a, b)) # Element-wise maximum print(pmax(a, b)) # Element-wise minimum print(pmin(a, b)) Executing the above code outputs the following result: 35 10 25 35 5 20 30 [![Image 4: R Language Examples](#) R Language Examples](#)
← R Func NcharR Func Match β†’