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

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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)), ]




1 Answer
1



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





I couldn't get the logic behind the sum(x>300)>=2 though.
– user1883491
Aug 7 at 19:37


sum(x>300)>=2





x > 300 returns a logical vector with TRUE/FALSE for each value in the row. Summing up these logical values returns the number of values in each row that are > 300
– bouncyball
Aug 7 at 19:38


x > 300






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

oM WP3,zZ tejiZD8 aEfi,nH SNO zqlIxdfqusDY,Bdrvo Y49CVD8ZFH8Gu7c QKea
gtHZ,y6YB,r,bEESpQjiGJXHB2,AVd z,FBvCzXC6hskobDIa7s9PjJVlLChFRyLy bFhWgKFF,mutYSTOd5AvhgtHRaBxCWb3

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3