I want to do do something like pnorm %in% c(pnorm, pt, pchisq)
, but this doesn't work.
What could I do instead?
A solution with purrr:
has_element(c(pt,pnorm,pchisq),pnorm)
#[1] TRUE
has_element(c(pt,pchisq),pnorm)
#[1] FALSE
pnorm %in% c(pnorm, pt, pchisq)
results in error :
Error in match(x, table, nomatch = 0L) : 'match' requires vector arguments
Indeed %in%
calls match
and ?match
tells us (slightly reformatted):
x : vector or NULL: the values to be matched.
We can make it work by making your function a 1 element list :
list(pnorm) %in% c(pnorm, pt, pchisq)
# [1] TRUE
However, a more idiomatic way of doing what you want is to apply the function identical on your list of functions, it will loop on your function elements and check for each one if it's identical to pnorm
:
sapply(c(pnorm, pt, pchisq), identical, pnorm)
# [1] TRUE FALSE FALSE
To get a your desired output, wrap inside any :
any(sapply(c(pnorm, pt, pchisq), identical, pnorm))
# [1] TRUE
These 2 methods are not strictly equivalent however because identical is more strict and will check the environment while the first solution will only check the arguments and body (ie the output of as.list(fun)),
see :
pnorm2 <- function (q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
.Call(C_pnorm, q, mean, sd, lower.tail, log.p)
environment(pnorm2)
# <environment: R_GlobalEnv>
environment(pnorm)
# <environment: namespace:stats>
any(sapply(c(pnorm, pt, pchisq), identical, pnorm2))
# [1] FALSE
list(pnorm2) %in% c(pnorm, pt, pchisq)
# [1] TRUE
`%inl%` <- function(z,l) !is.null(Find(function(x) identical(x,z),l))
pnorm %inl% c(pt,pchisq)
#[1] FALSE
pnorm %inl% c(pt,pnorm,pchisq)
#[1] TRUE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.