Get Minimum value of a column in R

Minimum value of a column in R can be calculated by using min() function. min() Function takes column name as argument and calculates the Minimum value of that column. Minimum of single column in R, Minimum of multiple columns in R using dplyr. Let’s see how to calculate Minimum value in R with an example.

  • Minimum of the single column in R – min() function
  • Minimum  values of multiple columns in R
  • Minimum  values of Multiple columns in R using dplyr
  • Find Minimum value of the column by column name
  • Find Minimum value of the column by column position
  • Get Row wise minimum in R

Let’s first create the dataframe.

### Create Data Frame
df1 = data.frame(Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), 
                 Grade_score=c(4,6,2,9,5,7,8),
                 Mathematics1_score=c(45,78,44,89,66,49,72),
                 Science_score=c(56,52,45,88,33,90,47))
df1

So the resultant dataframe will be

Get Minimum value of a column in R 1

 

Get minimum value of the column in R: minimum of the column by column name:

Max() function in R 2

Method 1: Get minimum value of the column by column name

# Get minimum value of the column by column name

min(df1$Mathematics1_score)

Result:

[1] 44

 

Get minimum value of the column in R: min of the column by column position

Method 2:Get minimum value of the column by column position

# Get minimum value of the column by column position

min(df1[,3])

Result:

[1] 44

 

 

Get Minimum of multiple columns R using colMins() : Method 1

ColMins() Function along with sapply() is used to get the minimum value of multiple columns. Dataframe is passed as an argument to ColMins() Function. Minimum of numeric columns of the dataframe is calculated. ColMins() Function is applied only to matrix, so the dataframe is converted to matrix using as.matrix() function.

# Get Min of the multiple columns

colMins(as.matrix(df1[sapply(df1, is.numeric)]))

Minimum of numeric columns of the dataframe will be

Get Minimum value of a column in R 5

 

 

Get Min of multiple columns in R using Dplyr : Method 2

summarise_if() Function along with is.numeric is used to get the min of the multiple column . With the help of summarise_if() Function, Min of numeric columns of the dataframe is calculated.

# Get Min of the multiple columns using dplyr

library(dplyr)
df1 %>% summarise_if(is.numeric, min)

Min of numeric columns of the dataframe will be

 

Get Minimum value of a column in R 5

 

Get Row wise min in R

Let’s calculate the row wise minimum of mathematics1_score and science_score as shown below


# Get Row wise min in R

df1$Min_score = rowMins(as.matrix((df1[,c(3,4)]) ))
df1

so the resultant dataframe with row wise min calculated will be

Get Minimum value of a column in R 4


Other Related Topics:

 

 

                                                                                                         

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.