I have a big data frame called df(500k rows & 50 columns). I need to filter this data frame based in two conditions at the same time (i.e: I need to remove the rows that have values grater than 0.6 and values less than 0.1 for all columns at the same time)
Asked
Active
Viewed 76 times
-2
-
3Possible duplicate of [Filter data.frame rows by a logical condition](https://stackoverflow.com/questions/1686569/filter-data-frame-rows-by-a-logical-condition) – divibisan Jun 06 '18 at 18:56
-
1This would be a much better question with a small example input with desired output. – Gregor Thomas Jun 06 '18 at 19:20
2 Answers
0
Not sure I understood what you meant by "for all columns at the same time" but if you meant remove rows that have a value in any column that is greater than 0.6 or smaller than 0.1 then a solution
keep <- apply(df,1, function(x){
all(x < 0.6 & x> 0.1)
})
filtered_df <- df[keep,]

GordonShumway
- 1,980
- 13
- 19
0
If we need to filter
for all columns, use filter_all
with all_vars
library(dplyr)
df1 %>%
filter_all(all_vars(!(. > 0.6 & . < 0.1)))

akrun
- 874,273
- 37
- 540
- 662