测试单个数值向量的所有元素之间的相等性 [英] Test for equality among all elements of a single numeric vector

查看:57
本文介绍了测试单个数值向量的所有元素之间的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试向量的所有元素是否彼此相等.我想出的解决方案似乎有些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

一个让我包括一个用于评估要素之间平等"的容差值的解决方案将是避免

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.

这篇关于测试单个数值向量的所有元素之间的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆