我正在尝试测试向量的所有元素是否彼此相等。我想出的解决方案似乎有些round回,都涉及检查 length()。
I'm trying to test whether all elements of a vector are equal to one another. The solutions I have come up with seem somewhat roundabout, both involving checking length().
x <- c(1, 2, 3, 4, 5, 6, 1) # FALSE y <- rep(2, times = 7) # TRUE具有 unique():
length(unique(x)) == 1 length(unique(y)) == 1带有 rle():
length(rle(x)$values) == 1 length(rle(y)$values) == 1一个让我包括一个用于评估元素之间平等的容差值的解决方案将是避免 FAQ 7.31 问题。
A solution that would let me include a tolerance value for assessing 'equality' among elements would be ideal to avoid FAQ 7.31 issues.
是否存在针对我完全忽略的测试类型的内置函数? identical()和 all.equal()比较两个R对象,因此它们在这里不起作用。
Is there a built-in function for type of test that I have completely overlooked? identical() and all.equal() compare two R objects, so they won't work here.
编辑1
以下是一些基准测试结果。使用代码:
Here are some benchmarking results. Using the code:
library(rbenchmark) John <- function() all( abs(x - mean(x)) < .Machine$double.eps ^ 0.5 ) DWin <- function() {diff(range(x)) < .Machine$double.eps ^ 0.5} zero_range <- function() { if (length(x) == 1) return(TRUE) x <- range(x) / mean(x) isTRUE(all.equal(x[1], x[2], tolerance = .Machine$double.eps ^ 0.5)) } x <- runif(500000); benchmark(John(), DWin(), zero_range(), columns=c("test", "replications", "elapsed", "relative"), order="relative", replications = 10000)结果为:
test replications elapsed relative 2 DWin() 10000 109.415 1.000000 3 zero_range() 10000 126.912 1.159914 1 John() 10000 208.463 1.905251所以它看起来像 diff(range(x) )< .Machine $ double.eps ^ 0.5 是最快的。
So it looks like diff(range(x)) < .Machine$double.eps ^ 0.5 is fastest.
推荐答案我使用这种方法,最小值和最大值,然后除以平均值:
I use this method, which compares the min and the max, after dividing by the mean:
# Determine if range of vector is FP 0. zero_range <- function(x, tol = .Machine$double.eps ^ 0.5) { if (length(x) == 1) return(TRUE) x <- range(x) / mean(x) isTRUE(all.equal(x[1], x[2], tolerance = tol)) }如果您更认真地使用它,则可能需要在计算范围和均值之前删除缺失值。
If you were using this more seriously, you'd probably want to remove missing values before computing the range and mean.
更多推荐
测试单个向量的所有元素之间的相等性
发布评论