测试单个向量的所有元素之间的相等性

编程入门 行业动态 更新时间:2024-10-19 00:30:53
本文介绍了测试单个向量的所有元素之间的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

测试单个向量的所有元素之间的相等性

本文发布于:2023-11-29 17:08:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   元素   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!