Ramakrishna S
Ramakrishna S

Reputation: 437

Filtering all rows if any value in a row is less than a threshold value

I would like to remove all rows if any value of the row is less than 0.05. Any suggestions? I need dplyr and base R simple subset solutions.

library(magrittr)
text = '
   INNO RISK  PRO  AMB  MKT   IP
1  0.00 0.01 0.00 0.00 0.19 0.24
2  1.00 0.83 0.04 0.48 0.60 0.03
3  0.01 0.07 0.79 0.05 0.19 0.00
4  0.99 0.99 0.92 0.86 0.01 0.10
5  0.72 0.93 0.28 0.48 1.00 0.90
6  0.96 1.00 1.00 0.86 1.00 0.75
7  0.02 0.07 0.01 0.86 0.60 0.00
8  0.02 0.01 0.01 0.12 0.60 0.24
9  0.02 0.93 0.92 0.02 0.19 0.90
10 0.99 0.97 0.92 0.86 0.99 0.90'
d10 = textConnection(text) %>% read.table(header = T)

Created on 2020-11-28 by the reprex package (v0.3.0)

Upvotes: 1

Views: 229

Answers (1)

akrun
akrun

Reputation: 887108

We can use rowSums

d10[!rowSums(d10 < 0.05),]
#     INNO RISK  PRO  AMB  MKT   IP
#5  0.72 0.93 0.28 0.48 1.00 0.90
#6  0.96 1.00 1.00 0.86 1.00 0.75
#10 0.99 0.97 0.92 0.86 0.99 0.90

Or with dplyr

library(dplyr)
d10 %>% 
     filter(across(everything(), ~ . >= 0.05))
#    INNO RISK  PRO  AMB  MKT   IP
#5  0.72 0.93 0.28 0.48 1.00 0.90
#6  0.96 1.00 1.00 0.86 1.00 0.75
#10 0.99 0.97 0.92 0.86 0.99 0.90

Upvotes: 1

Related Questions