0

I have 4 by 5 random matrix. How can I set the elements that are less than zero in columns 1 and 3 to zero in R? I tried to use replace() function.

r <- rnorm(20, mean=2, sd=2)
r1 <- matrix(r, ncol=5, nrow=4, byrow=T)
replace(r1, r1[,c(1,3)] < 0, 0) 

Thank you for any help!

markus
  • 25,843
  • 5
  • 39
  • 58
Pol
  • 117
  • 3

2 Answers2

0

You were close, the code should be

r1[, c(1,3)] <- replace(r1[, c(1,3)], r1[, c(1,3)] < 0, 0)
#          [,1]     [,2]     [,3]        [,4]     [,5]
#[1,] 0.2061709 2.369698 5.175691 -0.26075135 1.839496
#[2,] 2.2648406 3.415909 1.520604  5.96894787 1.722426
#[3,] 2.8353015 3.963506 1.214609 -0.07933795 5.564458
#[4,] 0.0000000 3.757209 2.071613  4.02565738 2.864530

Within replace you need to specify cols 1 and 3 for the x argument as well as the list argument. The problem with your code is the that list argument get's recycled and you'd get wrong results.

replace(r1,r1[,c(1,3)] <0, 0)
#          [,1]     [,2]     [,3]        [,4]     [,5]
#[1,] 0.2061709 2.369698 5.175691 -0.26075135 1.839496
#[2,] 2.2648406 3.415909 1.520604  5.96894787 1.722426
#[3,] 2.8353015 3.963506 1.214609 -0.07933795 5.564458
#[4,] 0.0000000 3.757209 0.000000  4.02565738 0.000000

data

set.seed(2)
(r1 <- matrix(rnorm(20, mean=2, sd=2), ncol=5, nrow=4, byrow=T))
#           [,1]     [,2]     [,3]        [,4]     [,5]
#[1,]  0.2061709 2.369698 5.175691 -0.26075135 1.839496
#[2,]  2.2648406 3.415909 1.520604  5.96894787 1.722426
#[3,]  2.8353015 3.963506 1.214609 -0.07933795 5.564458
#[4,] -2.6221382 3.757209 2.071613  4.02565738 2.864530
markus
  • 25,843
  • 5
  • 39
  • 58
0

I actually like using min and max functions for things like this. In this case, we want pmax in R.

r1[,c(1,3)] <- pmax(r1[,c(1,3)], 0)

Or a little more convoluted:

r1[,c(1,3)][r1[,c(1,3)]<0] <- 0

Benchmark

And just to satisfy the question, looks like that more convoluted way is the fastest, but replace is pretty quick too.

library(microbenchmark)

microbenchmark(
  r1[,c(1,3)] <- replace(r1[, c(1,3)], r1[, c(1,3)] < 0, 0),
  r1[,c(1,3)][r1[,c(1,3)]<0] <- 0,
  r1[,c(1,3)] <- pmax(r1[,c(1,3)], 0)
)

Unit: microseconds
                                                          expr    min      lq     mean median      uq    max neval
 r1[, c(1, 3)] <- replace(r1[, c(1, 3)], r1[, c(1, 3)] < 0, 0)  3.601  3.9005  4.97602  4.201  4.5020 25.501   100
                         r1[, c(1, 3)][r1[, c(1, 3)] < 0] <- 0  3.200  3.3020  4.75701  3.601  3.9020 24.201   100
                       r1[, c(1, 3)] <- pmax(r1[, c(1, 3)], 0) 12.601 13.1010 17.42398 13.901 15.5015 49.601   100