I was trying to use all.equal inside subset() and getting errors
because I was comparing vectors of unequal length (a column versus a
constant). I defined an function that did not throw the same errors,
but wondered if something similar (and probably better designed than
my noobish attempt) had already been described. Searching on all.equal
and infix did not produce any results with RSiteSearch.
set.seed(23)
df1 <- data.frame(a= rnorm(10), b=letters[1:10])
df1
subset(df1, all.equal(a, 0.19321233, tol=0.0001))
#Error in subset.data.frame(df1, all.equal(a, 0.19321233, tol =
1e-04)) :
# 'subset' must evaluate to logical
all.equal(df1$a, 0.19321233, tol=0.0001)
# [1] "Numeric: lengths (10, 1) differ"
#----
`%=%` <- function(x,y) {abs(x - y) <= .Machine$double.eps ^ 0.5}
#----
df1$a %=% 0.19321233
# [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
subset(df1, a %=% 0.19321233)
# a b
# 0.1932123 a
(Was a bit surprised that the condition was satisfied with such a low
tolerance, but was happy with the function working as expected.)
--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.