Reputation: 45
I don't want to remove all negative elements, that I can do. But, I want to remove all rows even if it contains only one negative value in my data frame.
> head(kosoyCorrected)
BER1_EW BER2_EW BER3_EW BER4_EW BER5_EW BER6_EW
1 7.087613184 7.09928796 7.087194381 6.96315939 7.086734346 7.09934523
2 4.599450934 3.89325300 4.160360141 4.81419817 4.090161726 4.34070903
3 0.100477184 0.02351617 -0.001589346 0.01072809 0.023073244 -0.06953596
4 0.132531627 0.09994992 0.123564389 0.13849246 0.217604484 0.09164854
5 -0.005220038 0.07117798 0.133075865 0.05525490 -0.003944601 0.10597363
6 0.107204375 0.11755171 0.060868101 0.14361525 0.109494893 0.13081894
Here, for example, I want to remove entire Row 3 and Row 5, not just the 4 negative values in these rows.
Thanks in advance!
Upvotes: 3
Views: 1828
Reputation: 887088
We can use rowSums
to create a logical vector for subsetting the rows
subset(kosoyCorrected, !rowSums(kosoyCorrected < 0))
# BER1_EW BER2_EW BER3_EW BER4_EW BER5_EW BER6_EW
#1 7.0876132 7.09928796 7.0871944 6.9631594 7.0867343 7.09934523
#2 4.5994509 3.89325300 4.1603601 4.8141982 4.0901617 4.34070903
#4 0.1325316 0.09994992 0.1235644 0.1384925 0.2176045 0.09164854
#6 0.1072044 0.11755171 0.0608681 0.1436152 0.1094949 0.13081894
Or another option is Reduce
subset(kosoyCorrected, Reduce(`&`, lapply(kosoyCorrected, `>`, 0)))
Or a vectorized option in dplyr
with filter_all
library(dplyr)
kosoyCorrected %>%
filter_all( all_vars(. > 0))
# BER1_EW BER2_EW BER3_EW BER4_EW BER5_EW BER6_EW
#1 7.0876132 7.09928796 7.0871944 6.9631594 7.0867343 7.09934523
#2 4.5994509 3.89325300 4.1603601 4.8141982 4.0901617 4.34070903
#4 0.1325316 0.09994992 0.1235644 0.1384925 0.2176045 0.09164854
#6 0.1072044 0.11755171 0.0608681 0.1436152 0.1094949 0.13081894
Or in the newer version with across
kosoyCorrected %>%
filter(across(everything(), ~ . > 0))
kosoyCorrected <- structure(list(BER1_EW = c(7.087613184, 4.599450934, 0.100477184,
0.132531627, -0.005220038, 0.107204375), BER2_EW = c(7.09928796,
3.893253, 0.02351617, 0.09994992, 0.07117798, 0.11755171), BER3_EW = c(7.087194381,
4.160360141, -0.001589346, 0.123564389, 0.133075865, 0.060868101
), BER4_EW = c(6.96315939, 4.81419817, 0.01072809, 0.13849246,
0.0552549, 0.14361525), BER5_EW = c(7.086734346, 4.090161726,
0.023073244, 0.217604484, -0.003944601, 0.109494893), BER6_EW = c(7.09934523,
4.34070903, -0.06953596, 0.09164854, 0.10597363, 0.13081894)),
class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
Upvotes: 4
Reputation: 101327
Another base R option using rowSums
+ sign
subset(kosoyCorrected,rowSums(sign(kosoyCorrected))==ncol(kosoyCorrected))
giving
BER1_EW BER2_EW BER3_EW BER4_EW BER5_EW BER6_EW
1 7.0876132 7.09928796 7.0871944 6.9631594 7.0867343 7.09934523
2 4.5994509 3.89325300 4.1603601 4.8141982 4.0901617 4.34070903
4 0.1325316 0.09994992 0.1235644 0.1384925 0.2176045 0.09164854
6 0.1072044 0.11755171 0.0608681 0.1436152 0.1094949 0.13081894
Upvotes: 1
Reputation: 39858
One dplyr
option could be:
df %>%
rowwise() %>%
filter(all(c_across(everything()) >= 0))
BER1_EW BER2_EW BER3_EW BER4_EW BER5_EW BER6_EW
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 7.09 7.10 7.09 6.96 7.09 7.10
2 4.60 3.89 4.16 4.81 4.09 4.34
3 0.133 0.0999 0.124 0.138 0.218 0.0916
4 0.107 0.118 0.0609 0.144 0.109 0.131
Or:
df %>%
rowwise() %>%
filter(min(c_across(everything())) >= 0)
Upvotes: 2