Negate a predicate function.

negate(.p)

Arguments

.p

A single predicate function, a formula describing such a predicate function, or a logical vector of the same length as .x. Alternatively, if the elements of .x are themselves lists of objects, a string indicating the name of a logical element in the inner lists. Only those elements where .p evaluates to TRUE will be modified.

Value

A new predicate function.

Examples

negate("x")
#> <composed> #> 1. function (x, ...) #> pluck(x, "x", .default = NULL) #> <environment: 0x7556340> #> #> 2. function (.x) #> !.x #> <bytecode: 0x5300478> #> <environment: 0x73610f0>
negate(is.null)
#> <composed> #> 1. function (x) #> .Primitive("is.null")(x) #> #> 2. function (.x) #> !.x #> <bytecode: 0x5300478> #> <environment: 0x5440758>
negate(~ .x > 0)
#> <composed> #> 1. <lambda> #> function (..., .x = ..1, .y = ..2, . = ..1) #> .x > 0 #> <environment: 0x63727a8> #> attr(,"class") #> [1] "rlang_lambda_function" #> #> 2. function (.x) #> !.x #> <bytecode: 0x5300478> #> <environment: 0x1b97998>
x <- transpose(list(x = 1:10, y = rbernoulli(10))) x %>% keep("y") %>% length()
#> [1] 3
x %>% keep(negate("y")) %>% length()
#> [1] 7
# Same as x %>% discard("y") %>% length()
#> [1] 7