jeudi 21 janvier 2021

Assign NA in conditional for loop

I'm trying to create a function that assigns NA to all columns that return TRUE on the condition ((sum of column values)/total number of rows) < 0.1

I tried something like this:

matrix <- cbind(c(0,1,1,1,0,1,0,1,0,1,0,1),
            c(1,1,1,1,0,1,0,0,1,0,0,1), 
            c(1,0,0,0,0,1,0,1,1,1,1,0),
            c(0,0,0,0,0,0,0,0,0,0,0,0))

filter10 <- function(a) {
              for (i in seq(ncol(a))) {
                if ((sum(a[,i])/nrow(a)) < 0.1) 
                  a[,i] = NA
              }
}

but nothing happens to the matrix after

> filter10(matrix)
> 

Thank you

Aucun commentaire:

Enregistrer un commentaire