I have a data frame like so:
df
A B C D E F
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 24. 6. 16. 5. 1.20 6.
2 21. 2. 19. 2. 1.09 2.
3 12. 2. 12. 79. 0.860 2.
4 39. 7. 39. 39. 1.90 7.
5 51. 1. 82. 27. 2.30 1.
6 24. 9. 24. 40. 1.60 9.
7 48. 1. 32. 5. 1.60 1.
8 44. 1. 44. 12. 1.70 1.
9 14. 1. 18. 6. 0.880 1.
10 34. 2. 51. 5. 2.70 2.
# ... with 4,688 more rows
I would like to filter this data frame according to a list, such that for each column of df the minimum and maximum would be according to the minimum and maximum of the list Neighb:
[[1]]
[1] 15.7 15.9 16.0 16.1 16.2
[[2]]
[1] 0 1 2 3 4
[[3]]
[1] 15.0 15.3 16.0 16.3 16.5
[[4]]
[1] 3 4 5 6 7
[[5]]
[1] 1.08 1.09 1.10 1.11 1.12
[[6]]
[1] 0 1 2 3 4
Is there a way to do this efficiently with dplyr/base R? Until now I used loops and filtered each column of df at a time
We can use Map from base R
Map(function(x, y) x[x >= min(y) & x <= max(y)], df, Neighb)
#$A
#numeric(0)
#$B
#[1] 2 2 1 1 1 1 2
#$C
#[1] 16
#$D
#[1] 5 5 6 5
#$E
#[1] 1.09
#$F
#[1] 2 2 1 1 1 1 2
If we need to filter the dataset based on the logical index, i.e. rows that have all TRUE based on the comparison with 'Neighb'
df[Reduce(`&`, Map(function(x, y) x >= min(y) & x <= max(y), df, Neighb)), ]
and if it is any TRUE
df[Reduce(`|`, Map(function(x, y) x >= min(y) & x <= max(y), df, Neighb)),]
df <- structure(list(A = c(24, 21, 12, 39, 51, 24, 48, 44, 14, 34),
B = c(6, 2, 2, 7, 1, 9, 1, 1, 1, 2),
C = c(16, 19, 12, 39, 82, 24, 32, 44, 18, 51),
D = c(5, 2, 79, 39, 27, 40, 5, 12, 6, 5),
E = c(1.2, 1.09, 0.86, 1.9, 2.3, 1.6, 1.6, 1.7, 0.88, 2.7),
F = c(6, 2, 2, 7, 1, 9, 1, 1, 1, 2)),
.Names = c("A","B", "C", "D", "E", "F"),
class = "data.frame",
row.names = c(NA, -10L))
Neighb <- list(c(15.7, 15.9, 16.0, 16.1, 16.2),
c(0, 1, 2, 3, 4),
c(15.0, 15.3, 16.0, 16.3, 16.5),
c(3, 4, 5, 6, 7),
c(1.08, 1.09, 1.10, 1.11, 1.12),
c(0, 1, 2, 3, 4))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With