Select rows without missing values ​​in R

I am a new user for R and for the loop. I am trying to take a sample from the data and check if there is a Colinear column. I want to write in this iteration that a colinear column exists and write it to a vector (baditr). In addition, I would like to print a line indicating that "colinearity is at iteration i". Then I would like the code to go to the second iteration and keep working. For each iteration, I would like the code to store the sum of the columns in the corresponding row of the matrix.

My problem is that I get NA for bad iterations. My intention is to prevent bad iterations from being included in my matrix. Here is my code:

a0=rep(1,40)
a=rep(0:1,20)
b=c(rep(1,20),rep(0,20))
c0=c(rep(0,12),rep(1,28))
c1=c(rep(1,5),rep(0,35))
c2=c(rep(1,8),rep(0,32))
c3=c(rep(1,23),rep(0,17))
da=matrix(cbind(a0,a,b,c0,c1,c2,c3),nrow=40,ncol=7)
sing <- function(nrw){
  sm <- matrix(NA,nrow=nrw,ncol=ncol(da))
  baditr <- NULL
  for(i in 1:nrw){
    ind <- sample(1:nrow(da), nrow(da),replace =TRUE)
    smdat <- da[ind,]
    evals <- eigen(crossprod(smdat))$values
    if(any(abs(evals) < 1e-7)){
      baditr <- c(baditr,i)
      cat("singularity occurs at", paste(i),"\n")
      next
    }
  sm[i,] <- apply(smdat,2,sum)
  }
  return(sm)
}
sing(20)

I will get the following result:

singularity occurs at 9 
singularity occurs at 13 
      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
 [1,]   40   23   22   25    5    8   26
 [2,]   40   20   18   30    4    7   22
 [3,]   40   19   24   28    6    7   25
 [4,]   40   19   22   30    6    9   26
 [5,]   40   12   26   26    8   13   30
 [6,]   40   17   16   27    7   10   19
 [7,]   40   20   17   33    3    5   19
 [8,]   40   22   19   28    4    9   23
 [9,]   NA   NA   NA   NA   NA   NA   NA
[10,]   40   21   24   28    3    6   27
[11,]   40   21   16   31    2    4   22
[12,]   40   21   21   26    3    6   23
[13,]   NA   NA   NA   NA   NA   NA   NA
[14,]   40   18   16   29    2    7   22
[15,]   40   24   18   30    6    9   21
[16,]   40   23   18   29    4    8   21
[17,]   40   17   25   25    3    8   29
[18,]   40   22   28   23    9   14   30
[19,]   40   25   23   25    7   11   30
[20,]   40   20   23   27    7   10   26

I would like my matrix to look like this:

singularity occurs at 9 
singularity occurs at 13 
      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
 [1,]   40   23   22   25    5    8   26
 [2,]   40   20   18   30    4    7   22
 [3,]   40   19   24   28    6    7   25
 [4,]   40   19   22   30    6    9   26
 [5,]   40   12   26   26    8   13   30
 [6,]   40   17   16   27    7   10   19
 [7,]   40   20   17   33    3    5   19
 [8,]   40   22   19   28    4    9   23
[10,]   40   21   24   28    3    6   27
[11,]   40   21   16   31    2    4   22
[12,]   40   21   21   26    3    6   23
[14,]   40   18   16   29    2    7   22
[15,]   40   24   18   30    6    9   21
[16,]   40   23   18   29    4    8   21
[17,]   40   17   25   25    3    8   29
[18,]   40   22   28   23    9   14   30
[19,]   40   25   23   25    7   11   30
[20,]   40   20   23   27    7   10   26

, , (, 50 ), , . , 50 , , 50 , , 100 .

. .

+5
1

sm, NA, complete.cases(). sm[complete.cases(sm),]. TRUE/FALSE, R FALSE.

, , - baditers . , baditers, , , ... , ?

Update

complete.cases(). . , baditr, , .

sing <- function(nrw){
  sm <- matrix(NA,nrow=nrw,ncol=ncol(da))
  #baditr <- NULL
  for(i in 1:nrw){
    ind <- sample(1:nrow(da), nrow(da),replace =TRUE)
    smdat <- da[ind,]
    evals <- eigen(crossprod(smdat))$values
    if(any(abs(evals) < 1e-7)){
      #baditr <- c(baditr,i)
      cat("singularity occurs at", paste(i),"\n")
      next
    }
    sm[i,] <- apply(smdat,2,sum)
  }
  return(sm[complete.cases(sm),])
}

, dim() , #rows #columns :

> dim(sing(20))
singularity occurs at 6 
[1] 19  7

, 19 7 , - ?

, append write.table() ? , If TRUE, the output is appended to the file. If FALSE, any existing file of the name is destroyed.

2

append = TRUE write.table()

#Matrix 1 definition and write to file
x <- matrix(1:9, ncol = 3)
write.table(x, "out.txt", sep = "\t", col.names = TRUE, row.names = FALSE)
#Matrix 2 definition and write to same file with append = TRUE
x2 <- matrix(10:18, ncol = 3)
write.table(x2, "out.txt", sep = "\t", col.names = FALSE, row.names = FALSE, append = TRUE)
#read consolidated data back in to check if it right
x3 <- read.table("out.txt", header = TRUE)

  V1 V2 V3
1  1  4  7
2  2  5  8
3  3  6  9
4 10 13 16
5 11 14 17
6 12 15 18
+6

All Articles