r replacing values inferior to the mean of each column in a matrix

52 views Asked by At

I have a huge matrix in which I would like to replace the values inferior to the mean (or median) of each column with an NA. For example, with this matrix:

set.seed(1)
ex <- matrix(data = round(runif(12), 1), nrow = 4, ncol = 3)
ex
     [,1] [,2] [,3]
[1,]  0.3  0.2  0.6
[2,]  0.4  0.9  0.1
[3,]  0.6  0.9  0.2
[4,]  0.9  0.7  0.2

I would like to get:

for(i in 1:ncol(ex)){
  ex[, i][ex[, i] < colMeans(ex)[i]] <- NA
}
ex
     [,1] [,2] [,3]
[1,]   NA   NA  0.6
[2,]   NA  0.9   NA
[3,]  0.6  0.9   NA
[4,]  0.9  0.7   NA

The code above uses a for-loop and I would like to have a faster vectorized version.

3

There are 3 answers

0
Ronak Shah On BEST ANSWER

We can use sweep.

ex[sweep(ex, 2, colMeans(ex), `<`)] <- NA
ex

#     [,1] [,2] [,3]
#[1,]   NA   NA  0.6
#[2,]   NA  0.9   NA
#[3,]  0.6  0.9   NA
#[4,]  0.9  0.7   NA

Or with some transposing

ex[t(t(ex) < colMeans(ex))] <- NA

Since it's a matrix, we can also use apply columnwise

apply(ex, 2, function(x) replace(x, x < mean(x), NA))
0
ThomasIsCoding On

Here is another base R solution

ex <- replace(ex, ex < t(replicate(nrow(ex),colMeans(ex))),NA)
0
P. Denelle On

A comparison of the solutions proposed by @Ronak Shah and @ThomasIsCoding with a microbenchmark on a bigger matrix gives the following result:

# Generate matrix
set.seed(1)
ex <- matrix(data = round(runif(100000), 1), nrow = 1000, ncol = 100)
ex
colMeans(ex)

# for-loop solution
ex2 <- ex
for(i in 1:ncol(ex2)){
  ex2[, i][ex2[, i] < colMeans(ex2)[i]] <- NA
}
ex2

# Solution with sweep
ex3 <- ex
ex3[sweep(ex3, 2, colMeans(ex3), "<")] <- NA
ex3

# Solution with replace
ex4 <- ex
ex4 <- replace(ex4, ex4 < t(replicate(nrow(ex4), colMeans(ex4))), NA)
ex4

# Transposing solution
ex5 <- ex
ex5[t(t(ex5) < colMeans(ex5))] <- NA
ex5

# Apply solution
ex6 <- ex
apply(ex6, 2, function(x) replace(x, x < mean(x), NA))
ex6

# Identical
all.equal(ex2, ex3, ex4, ex5, ex6)

# Microbenchmark
library(microbenchmark)
comp <- microbenchmark(
  for_loop = {
    ex2 <- ex
    for(i in 1:ncol(ex2)){
      ex2[, i][ex2[, i] < colMeans(ex2)[i]] <- NA
    }},

  sweep = {
    ex3 <- ex
    ex3[sweep(ex3, 2, colMeans(ex3), "<")] <- NA
  },

  replace = {
    ex4 <- ex
    ex4 <- replace(ex4, ex4 < t(replicate(nrow(ex4), colMeans(ex4))), NA)
  },

  transpose = {
    ex5 <- ex
    ex5[t(t(ex5) < colMeans(ex5))] <- NA
  },

  apply = {
    ex6 <- ex
    apply(ex6, 2, function(x) replace(x, x < mean(x), NA))
  }
)

library(ggplot2)
autoplot(comp)

enter image description here

They give identical results but sweep approach seems to be the fastest.