- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the row minimum excluding zero in R data frame returning 0 if all 0?
To find the row minimum excluding zero in R data frame returning 0 if all 0, we can follow the below steps −
- First of all, create a data frame.
- Then, find the row minimum by excluding zero using if function with apply function.
Example1
Create the data frame
Let's create a data frame as shown below −
x1<-sample(c(0,1,5),20,replace=TRUE) x2<-sample(c(0,10,20),20,replace=TRUE) x3<-sample(c(0,5,10),20,replace=TRUE) df1<-data.frame(x1,x2,x3) df1
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x1 x2 x3 1 0 10 0 2 0 0 0 3 1 10 0 4 5 20 0 5 5 20 10 6 1 10 5 7 0 10 10 8 5 0 10 9 0 20 0 10 1 20 10 11 1 10 5 12 5 0 5 13 0 0 0 14 0 0 5 15 5 20 0 16 5 20 0 17 5 20 10 18 5 10 10 19 0 20 5 20 1 10 10
Find row minimum by excluding zero returning 0 if all 0
Using apply function with if else function to find the row minimum by excluding zero returning 0 if all 0 −
x1<-sample(c(0,1,5),20,replace=TRUE) x2<-sample(c(0,10,20),20,replace=TRUE) x3<-sample(c(0,5,10),20,replace=TRUE) df1<-data.frame(x1,x2,x3) apply(df1,1,function(x) if(all(x==0)) 0 else min(x[x>0]))
Output
[1] 10 0 1 5 5 1 10 5 20 1 1 5 0 5 5 5 5 5 5 1
Example 2
Create the data frame
Let's create a data frame as shown below −
y1<-sample(c(0,50),20,replace=TRUE) y2<-sample(c(0,75),20,replace=TRUE) y3<-sample(c(0,90),20,replace=TRUE) df2<-data.frame(y1,y2,y3) df2
y1 y2 y3 1 50 75 90 2 0 5 0 3 0 75 0 4 50 0 90 5 50 0 90 6 0 0 0 7 0 75 90 8 50 75 0 9 0 75 90 10 50 0 0 11 0 0 0 12 50 0 0 13 0 0 0 14 0 0 90 15 50 75 90 16 50 75 0 17 50 75 90 18 50 75 0 19 0 75 90 20 50 0 90
Find row minimum by excluding zero returning 0 if all 0
Using apply function with if else function to find the row minimum by excluding zero returning 0 if all 0 −
y1<-sample(c(0,50),20,replace=TRUE) y2<-sample(c(0,75),20,replace=TRUE) y3<-sample(c(0,90),20,replace=TRUE) df2<-data.frame(y1,y2,y3) apply(df2,1,function(x) if(all(x==0)) 0 else min(x[x>0]))
Output
[1] 50 75 75 50 50 0 75 50 75 50 0 50 0 90 50 50 50 50 75 50
- Related Articles
- How to divide data frame row values by maximum value in each row excluding 0 in R?
- How to find the row sums by excluding a column in R data frame?
- How to find row minimum for an R data frame?
- How to find the smallest number in an R data frame column excluding values zero or less?
- How to calculate row means by excluding NA values in an R data frame?
- How to divide data frame rows in R by row minimum?
- How to divide matrix rows by maximum value in each row excluding 0 in R?
- How to find the row sums if NA exists in the R data frame?
- How to divide data.table object rows by maximum value in each row excluding 0 in R?
- How to find the column mean by excluding NA’s and if all values are NA then output NA in R data frame?
- How to find the minimum for each row based on few columns in an R data frame?
- How to find the column number of minimum values in each row for a data frame in R?
- How to find the row products for each row in an R data frame?
- How to find the column minimum if some columns are categorical in R data frame?
- How to find the number of columns where all row values are equal in R data frame?

Advertisements