ferrelwill
ferrelwill

Reputation: 821

How to print rows greater than N in at least X number of columns?

For example, how can I print rows greater than 300 in at least 2 columns?

The following code print rows greater than 300

mtcars[apply(mtcars[, -1], MARGIN = 1, function(x) any(x > 300)), ]

Upvotes: 0

Views: 42

Answers (1)

bouncyball
bouncyball

Reputation: 10761

A couple ways come to mind:

mtcars[apply(mtcars, 1, FUN = function(x) sum(x > 300) >= 2),]

or

mtcars[rowSums(mtcars > 300) >= 2,]

Which both return

#               mpg cyl disp  hp drat   wt qsec vs am gear carb
# Maserati Bora  15   8  301 335 3.54 3.57 14.6  0  1    5    8

Upvotes: 3

Related Questions