- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 create a column of first non-zero value in each row of an R data frame?
To create a column of first non-zero value in each row of an R data frame, we can follow the below steps −
First of all, create a data frame with some zero values.
Then, use apply function and a custom function to find the first non-zero in each row of the data frame.
Example
Create the data frame
Let’s create a data frame as shown below −
x<-sample(0:2,25,replace=TRUE) y<-sample(0:2,25,replace=TRUE) z<-sample(0:2,25,replace=TRUE) a<-sample(0:2,25,replace=TRUE) df<-data.frame(x,y,z,a) df
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x y z a 1 1 0 2 0 2 1 0 0 1 3 0 0 1 0 4 2 0 0 2 5 0 1 1 2 6 0 1 0 1 7 2 1 2 2 8 0 1 1 2 9 1 0 0 2 10 1 0 1 0 11 1 0 0 0 12 0 2 1 0 13 0 0 1 2 14 1 1 0 1 15 1 0 2 1 16 1 1 1 1 17 1 1 1 1 18 2 0 2 1 19 1 0 2 0 20 0 2 2 0 21 0 0 2 2 22 1 0 2 0 23 0 2 1 0 24 1 1 1 0 25 1 0 2 0
Find the first non-zero in each row of the data frame
Using apply function and a custom function to find the first non-zero in each row of the data frame df as shown below −
x<-sample(0:2,25,replace=TRUE) y<-sample(0:2,25,replace=TRUE) z<-sample(0:2,25,replace=TRUE) a<-sample(0:2,25,replace=TRUE) df<-data.frame(x,y,z,a) df$First_Non_zero<-apply(df,1, function(x) x[x !=0][1]) df
Output
x y z a First_Non_zero 1 1 2 1 0 1 2 0 2 2 2 2 3 0 0 2 1 2 4 0 2 1 1 2 5 2 0 0 1 2 6 2 1 0 2 2 7 1 2 1 1 1 8 1 1 0 1 1 9 1 2 2 0 1 10 2 0 1 0 2 11 2 1 1 1 2 12 0 0 0 1 1 13 2 2 1 1 2 14 0 1 1 1 1 15 1 1 1 1 1 16 2 1 0 1 2 17 1 0 1 0 1 18 1 2 1 1 1 19 0 2 1 1 2 20 0 1 1 2 1 21 0 1 2 0 1 22 1 0 2 0 1 23 1 1 1 0 1 24 0 0 2 0 2 25 2 2 1 2 2
- Related Articles
- How to create a column of first non-zero value in each row of a data.table object in R?
- How to create a column with column name for maximum value in each row of an R data frame?
- How to replace zero with first non-zero occurring at the next position in an R data frame column?
- Create a quartile column for each value in an R data frame column.
- How to add a new column to an R data frame with largest value in each row?
- How to create a row sum and a row product column in an R data frame?
- How to replace zero with previous value in an R data frame column?
- How to get row index based on a value of an R data frame column?
- How to create a new column with a subset of row sums in an R data frame?
- How to find the row and column index of a character value in an R data frame?
- Find the column name that contains value greater than a desired value in each row of an R data frame.
- How to select the first row for each level of a factor variable in an R data frame?
- How to create a new column with means of row values for each or some of the columns in an R data frame?
- How to create a data frame of the maximum value for each group in an R data frame using dplyr?
- How to create a row at the end an R data frame with column totals?

Advertisements