This chapter is dedicated to min and max function in R. min function in R – min(), is used to calculate the minimum of vector elements or minimum of a particular column of a dataframe. minimum of a group can also calculated using min() function in R by providing it inside the aggregate function. max(), is used to calculate the maximum of vector elements or maximum of a particular column of a dataframe. maximum of a group can also calculated using max() function in R by providing it inside the aggregate function. row wise maximum and minimum is calculated using max() and min() function with the help of dplyr package. we also calculates column wise maximum and column wise minimum. lets see an example of each.
- min() function in R computes the minimum value of a vector or data frame.
- max() function in R computes the maximum value of a vector or data frame.
- column wise maximum and minimum of the dataframe using max() and min() function.
- Row wise maximum and minimum of the dataframe in R using max() and min() function.
- maximum and minimum value of the group is calculated using max() and min() along with aggregate() and dplyr packages.
Syntax for min and Max function in R:
min(x, na.rm = FALSE)
max(x, na.rm = FALSE)
Max, Min, Sup, Inf We would like to begin by asking for the maximum of the function f(x) = (sinx)/x. An approximate graph is indicated below. Looking at the graph, it is clear that f(x) ≤ 1 for all x in the domain of f. Furthermore, 1 is the smallest number which is greater than all of f’s values. O y=(sin x)/x 1 Figure 1. Mar 07, 2019 We will have an absolute maximum (or minimum) at x = c x = c provided f (c) f (c) is the largest (or smallest) value that the function will ever take on the domain that we are working on. Also, when we say the “domain we are working on” this simply means the range of x x ’s that we have chosen to work with for a given problem. The MIN function returns the smallest value of the selected column. The MAX function returns the largest value of the selected column.
- x – is numeric or character vector
- na.rm – a logical indicating whether missing values should be removed.
Example of Max function in R:
output:
No Local Extrema Compare the f (x) f (x) values found for each value of x x in order to determine the absolute maximum and minimum over the given interval. The maximum will occur at the highest f (x) f (x) value and the minimum will occur at the lowest f (x) f (x) value. Absolute Maximum: (5,3) (5, 3). To find the maximum or minimum value of a quadratic function, start with the general form of the function and combine any similar terms. For example, if you’re starting with the function f(x) = 3x + 2x - x^2 + 3x^2 + 4, you would combine the x^2 and x terms to simplify and end up with f(x) = 2x^2 + 5x + 4.
Example of Max function in R with NA:
Max function doesn’t give desired output, If NAs are present in the vector. So it has to be handled by using na.rm=TRUE in max() function
output:
Example of Max function in R with character vector:
output:
Example of max() function in R dataframe:
Lets create the data frame to demonstrate max function – max() in r
so the resultant dataframe will be
maximum value of a column in R data frame using max() function :
max() function takes the column name as argument and calculates the maximum value of that particular column
so the resultant maximum value of the “Price” column will be
output:
column wise maximum using max() function:
max() function is applied to the required column through mapply() function, so that it calculates the maximum value of required columns as shown below.
so the resultant maximum value of “Price” and “Tax” columns will be
Maximum value of the column by group using max() function
aggregate() function along with the max() function calculates the maximum value of a group. here maximum of “Price” column, for “Item_Group” is calculated.
Item_group has three groups “Dairy”,”Fruit” & “Vegetable”. maximum price for each group is calculated as shown below
Row wise maximum using max() function along with dplyr
Row wise maximum is calculated with the help rowwise() function of dplyr package and max() function as shown below
row wise max of “Price” and “Tax” is calculated and populated for each row as shown below
Example of Min function in R:
output:
Example of Min function in R with NA:
Min function doesn’t give desired output, If NAs are present in the vector. so it has to be handled by using na.rm=TRUE in min() function
output:
Example of Min function in R with character vector:
output:
Example of min() function in R dataframe:
Lets create the data frame to demonstrate min function – min() in r
so the resultant dataframe will be
minimum value of a column in R data frame using min() function :
min() function takes the column name as argument and calculates the minimum value of that particular column
so the resultant minimum value of the “Price” column will be
output:
column wise minimum using min() function:
min() function is applied to the required column through mapply() function, so that it calculates the minimum value of required columns as shown below.
so the resultant minimum value of “Price” and “Tax” columns will be
Minimum value of the column by group using min() function
Max Min
aggregate() function along with the min() function calculates the minimum value of a group. here minimum of “Price” column, for “Item_Group” is calculated.
Item_group has three groups “Dairy”,”Fruit” & “Vegetable”. minimum price for each group is calculated as shown below
Min Max Normalization
Row wise minimum using min() function along with dplyr
Max Minghella Height
Row wise minimum is calculated with the help rowwise() function of dplyr package and min() function as shown below
Max Mini
row wise min of “Price” and “Tax” is calculated and populated for each row as shown below
For further understanding of min() and max() function in R using dplyr one can refer the dplyr documentation