Row wise minimum – row min in R dataframe

Row wise minimum of the dataframe in R or minimum value of each row is calculated using rowMins() function. Other method to get the row minimum in R is by using apply() function. row wise minimum of the dataframe is also calculated using dplyr package. rowwise() function of dplyr package along with the min function is used to calculate row wise min. we will be looking at the following examples

  • Row wise minimum of R dataframe using rowMins()
  • Row minimum of the dataframe using apply() function.
  • Row wise minimum of the dataframe using dplyr package.

row wise min in R using rowMins() and min() 21

First let’s create the dataframe


### Create Data Frame

df1 = data.frame( Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), 
 Mathematics1_score=c(62,47,55,74,32,77,86),
 Mathematics2_score=c(45,78,44,89,66,49,72),
 Science_score=c(56,52,45,88,33,90,47))

df1

df1 will be

Row wise minimum – row minimum in R dataframe 1

 

 

Row wise min in R dataframe using rowMins()

Let’s calculate the row wise min in R using rowMins() function as shown below. rowMins() function takes up column 2 to 4 and finds row wise minimum value.


## row wise minimum using rowMins()

install.packages("matrixStats")
library(matrixStats)

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

or


## row wise min using rowMins()

install.packages("matrixStats")
library(matrixStats)

df1$row_minimum = rowMins(as.matrix(df1[,c(-1)]))
df1

So the resultant dataframe will be

Row wise minimum – row minimum in R dataframe 2

 

 

Row wise minimum in R dataframe using apply() function

Let’s calculate the row wise min in R using apply() function as shown below.apply() function takes three arguments first argument is dataframe without first column and second argument is used to perform row wise operation (argument 1- row wise ; 2 – column wise ). third argument minimum function which calculates minimum values.  so here it performs row wise minimum.

### Row wise minimum using apply() function

df1$row_minimum = apply(df1[,-1], 1, min)
df1

So the resultant dataframe will be

Row wise minimum – row minimum in R dataframe 3

 

 

Row wise min of the dataframe in R using dplyr: Method 1

rowMins() function of matrixStats package. takes up the columns 2 to 4 and performs the row wise operation with NA values replaced to zero.  row wise min is calculated using pipe (%>%) operator of the dplyr package.

##### Dplyr row wise min
library(dplyr)
library(matrixStats)

df1 %>% replace(is.na(.), 0) %>% mutate(row_wise_min = rowMins(as.matrix(df1[,c(2,3,4)])))

So the resultant dataframe with row wise min calculated will be

Row wise minimum – row minimum in R dataframe 4

 

 

Row wise minimum of the dataframe using dplyr: Method 2

Row wise minimum is calculated with the help rowwise() function of dplyr package  and min() function as shown below

## row wise minimum using dplyr
 
library(dplyr)
library(matrixStats)

df1 %>%
  rowwise() %>%
  mutate(
    Min_price = min(c(Mathematics1_score,Mathematics2_score,Science_score))
  )

row wise minimum of “Mathematics1_score” , “Mathematics2_score” and “Science_score” is calculated and populated for each row as shown below

Row wise minimum – row minimum in R dataframe 5

For more Details kindly refer to matrixStats package in R


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.