makdoubble
makdoubble

Reputation: 1

How to remove columns that don't fit the parameters in R?

combo1 <- c(14,11)
combo2 <- c(3,10)
combo3 <- c(25,61)
combo4 <- c(12,13)
m <- matrix(c(combo1, combo2, combo3, combo4), ncol = 4)

I want to include columns that have both elements less than 16 and greater than 9. So i want my matrix to only show (14,11) and (12,13)

Upvotes: 0

Views: 28

Answers (1)

akrun
akrun

Reputation: 887223

We can use

m[,colSums(m < 16 & m > 9) == nrow(m)]
#       [,1] [,2]
#[1,]   14   12
#[2,]   11   13

If it is the other columns to keep

m[,colSums(m < 16 & m > 9) != nrow(m)]
#      [,1] [,2]
#[1,]    3   25
#[2,]   10   61

Upvotes: 1

Related Questions