Here is an option using tidyverse where we make use of the map and reduce functions from purrr to get a logical vector to extract (from magrittr) the rows of the original dataset
library(tidyverse)
library(magrittr)
df %>%
select(-one_of("P")) %>%
map(~ .> df$P) %>%
reduce(`|`) %>%
extract(df, .,)
# A tibble: 3 × 3
# P B C
# <dbl> <dbl> <dbl>
#1 2.0 2.1 2.2
#2 5.0 5.5 5.7
#3 1.4 2.0 1.5
This can also be converted to a function using the devel version of dplyr (soon to be released 0.6.0) which introduced quosures and unquote for evaluation. The enquo is almost similar to substitute from base R which takes the user input and convert it to quosure, one_of takes string arguments, so it can be converted to string with quo_name
funFilter <- function(dat, colToCompare){
colToCompare <- quo_name(enquo(colToCompare))
dat %>%
select(-one_of(colToCompare)) %>%
map(~ .> dat[[colToCompare]]) %>%
reduce(`|`) %>%
extract(dat, ., )
}
funFilter(df, P)#compare all other columns with P
# A tibble: 3 × 3
# P B C
# <dbl> <dbl> <dbl>
#1 2.0 2.1 2.2
#2 5.0 5.5 5.7
#3 1.4 2.0 1.5
funFilter(df, B) #compare all other columns with B
# A tibble: 4 × 3
# P B C
# <dbl> <dbl> <dbl>
#1 2 2.1 2.2
#2 4 3.0 3.8
#3 5 5.5 5.7
#4 6 1.2 5.0
We can also parse the expression
v1 <- setdiff(names(df), "P")
filter(df, !!rlang::parse_quosure(paste(v1, "P", sep=" > ", collapse=" | ")))
# A tibble: 3 × 3
# P B C
# <dbl> <dbl> <dbl>
#1 2.0 2.1 2.2
#2 5.0 5.5 5.7
#3 1.4 2.0 1.5
This can also be made into a function
funFilter2 <- function(dat, colToCompare){
colToCompare <- quo_name(enquo(colToCompare))
v1 <- setdiff(names(dat), colToCompare)
expr <- rlang::parse_quosure(paste(v1, colToCompare, sep= " > ", collapse= " | "))
dat %>%
filter(!!expr)
}
funFilter2(df, P)
# A tibble: 3 × 3
# P B C
# <dbl> <dbl> <dbl>
#1 2.0 2.1 2.2
#2 5.0 5.5 5.7
#3 1.4 2.0 1.5
funFilter2(df, B)
# A tibble: 4 × 3
# P B C
# <dbl> <dbl> <dbl>
#1 2 2.1 2.2
#2 4 3.0 3.8
#3 5 5.5 5.7
#4 6 1.2 5.0
Or another approach could be pmax
df %>%
filter(do.call(pmax, .) > P)
# A tibble: 3 × 3
# P B C
# <dbl> <dbl> <dbl>
#1 2.0 2.1 2.2
#2 5.0 5.5 5.7
#3 1.4 2.0 1.5
df[rowSums(df[-1] > df$P)>0,]- Sotosfilteris based on using columns of data, not rows of data. This is why you have a hard time fitting this intodplyr. - Paul Hiemstrasliceas shown here - Sotos