brandonEm
brandonEm

Reputation: 316

conditionally dropping columns from a matrix

I have a matrix:

set.seed(23)
dt.data <- unname(as.matrix(data.frame(rnorm(5,30,2),rnorm(5,-3,1),
              replicate(3,rnorm(5,5,1)))))
dt.data
#         [,1]      [,2]     [,3]     [,4]     [,5]
#[1,] 30.38642 -1.892510 5.218288 5.308137 5.835391
#[2,] 29.13064 -3.278086 3.953465 4.479822 4.433985
#[3,] 31.82653 -1.980795 4.711311 4.557686 5.788419
#[4,] 33.58678 -2.954563 5.481550 4.400687 3.834071
#[5,] 31.99321 -1.424220 3.783624 6.294578 4.469180

I'd like to drop all columns from the matrix whose mean is less than zero OR greater than 25 (i.e. - the first 2 columns above). I've been trying it with an apply function:

apply(dt.data,2,
  function(x) if ((mean(x,na.rm=TRUE))>25 | (mean(x,na.rm=TRUE)<0)) {
  dt.data<-dt.data[,-x]
})

I can use another apply function to check the means and drop them manually, but I'd like to generalize the procedure. The above function doesn't work, and throws the following error:

Error in dt.data[, -x] : only 0's may be mixed with negative subscripts

Any tips?

Upvotes: 0

Views: 58

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

colMeans should do it:

m <- colMeans(dt.data, na.rm=TRUE)
dt.data[, !(m > 25 | m < 0)]

Upvotes: 1

Related Questions