- 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-wise index of non-NA values in a matrix in R?
A matrix can also contain missing values and those missing values can be placed in a matrix by randomization as well, hence we cannot be sure about the positions of those values that are referred to as NA and the non-missing values. If we want to find the positions of the non-missing values in a matrix then apply function can be used where we can use which function to exclude NA values. Check out the below examples to understand how it works.
Example1
M1<−matrix(sample(c(NA,0,1),25,replace=TRUE),ncol=5) M1
Output
[,1] [,2] [,3] [,4] [,5] [1,] NA 1 NA 0 0 [2,] 1 1 NA 0 1 [3,] 1 0 1 1 0 [4,] 0 NA 1 1 0 [5,] 0 NA 0 0 NA
Example
apply(M1,1,function(x) which(!is.na(x)))
Output
[[1]] [1] 2 4 5 [[2]] [1] 1 2 4 5 [[3]] [1] 1 2 3 4 5 [[4]] [1] 1 3 4 5 [[5]] [1] 1 3 4
Example2
M2<− matrix(sample(c(NA,5,10,15,25),49,replace=TRUE),nrow=7) M2
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] NA NA 15 NA 10 15 5 [2,] 5 5 15 15 NA 10 25 [3,] NA 5 NA 10 25 10 10 [4,] 5 25 NA NA 10 5 NA [5,] 25 NA 15 5 15 10 5 [6,] 25 NA 10 NA 25 5 15 [7,] 10 5 25 NA 10 5 25
Example
apply(M2,1,function(x) which(!is.na(x)))
Output
[[1]] [1] 3 5 6 7 [[2]] [1] 1 2 3 4 6 7 [[3]] [1] 2 4 5 6 7 [[4]] [1] 1 2 5 6 [[5]] [1] 1 3 4 5 6 7 [[6]] [1] 1 3 5 6 7 [[7]] [1] 1 2 3 5 6 7
Example3
M3<−matrix(sample(c(NA,0:9),100,replace=TRUE),nrow=10) M3
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 0 2 2 2 0 4 5 9 4 [2,] 0 4 5 2 6 3 3 8 6 2 [3,] 8 NA 6 4 7 1 7 5 5 6 [4,] NA 4 7 4 5 4 NA 1 0 5 [5,] 7 4 3 1 1 0 5 1 7 7 [6,] 3 8 5 8 3 1 2 2 3 4 [7,] 5 4 3 NA 0 8 3 5 9 6 [8,] 7 7 7 6 1 0 7 NA 6 7 [9,] 9 5 3 2 4 2 3 4 3 8 [10,] 1 3 6 4 4 1 8 0 4 1
Example
apply(M3,1,function(x) which(!is.na(x)))
Output
[[1]] [1] 1 2 3 4 5 6 7 8 9 10 [[2]] [1] 1 2 3 4 5 6 7 8 9 10 [[3]] [1] 1 3 4 5 6 7 8 9 10 [[4]] [1] 2 3 4 5 6 8 9 10 [[5]] [1] 1 2 3 4 5 6 7 8 9 10 [[6]] [1] 1 2 3 4 5 6 7 8 9 10 [[7]] [1] 1 2 3 5 6 7 8 9 10 [[8]] [1] 1 2 3 4 5 6 7 9 10 [[9]] [1] 1 2 3 4 5 6 7 8 9 10 [[10]] [1] 1 2 3 4 5 6 7 8 9 10
Example4
M4<−matrix(sample(c(NA,rnorm(10,5,2)),36,replace=TRUE),nrow=6) M4
Output
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 5.8658749 3.4589638 9.448970 4.0246447 5.0317765 3.4589638 [2,] 9.4489696 9.1581552 5.865875 3.8337310 5.9396619 5.9396619 [3,] 4.0246447 7.5142676 NA 9.4489696 0.9925749 5.0317765 [4,] 7.5142676 5.8658749 3.833731 0.9925749 0.9925749 7.5142676 [5,] 0.9925749 7.5142676 NA 9.1581552 9.4489696 NA [6,] 5.0317765 0.9925749 3.833731 4.0246447 7.5142676 0.9925749
Example
apply(M4,1,function(x) which(!is.na(x)))
Output
[[1]] [1] 1 2 3 4 5 6 [[2]] [1] 1 2 3 4 5 6 [[3]] [1] 1 2 4 5 6 [[4]] [1] 1 2 3 4 5 6 [[5]] [1] 1 2 4 5 [[6]] [1] 1 2 3 4 5 6
Example5
M5<−matrix(sample(c(NA,rpois(10,5)),64,replace=TRUE),nrow=8) M5
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 10 0 7 5 3 10 10 10 [2,] 5 3 NA 5 5 5 5 5 [3,] 5 5 3 5 3 5 7 5 [4,] 3 10 5 10 NA NA 5 5 [5,] 7 7 7 5 7 10 8 7 [6,] 0 3 7 3 7 5 3 10 [7,] 0 NA 5 NA 7 NA 0 5 [8,] 5 10 8 7 3 NA 8 3
Example
apply(M5,1,function(x) which(!is.na(x)))
Output
[[1]] [1] 1 2 3 4 5 6 7 8 [[2]] [1] 1 2 4 5 6 7 8 [[3]] [1] 1 2 3 4 5 6 7 8 [[4]] [1] 1 2 3 4 7 8 [[5]] [1] 1 2 3 4 5 6 7 8 [[6]] [1] 1 2 3 4 5 6 7 8 [[7]] [1] 1 3 5 7 8 [[8]] [1] 1 2 3 4 5 7 8
Advertisements