作者:Metoo婧婧 | 来源:互联网 | 2023-09-25 06:55
Ihavealistofseveralvectors.Iwouldliketocheckwhetherallvectorsinthelistareequal.T
I have a list of several vectors. I would like to check whether all vectors in the list are equal. There's identical
which only works for pairwise comparison. So I wrote the following function which looks ugly to me. Still I did not find a better solution. Here's my RE:
我有几个向量的列表。我想检查列表中的所有向量是否相等。有相同的,只适用于成对比较。所以我写了下面这个看起来很难看的功能。我仍然找不到更好的解决方案。这是我的RE:
test_true <- list(a=c(1,2,3),b=c(1,2,3),d=c(1,2,3))
test_false <- list(a=c(1,2,3),b=c(1,2,3),d=c(1,32,13))
compareList <- function(li){
stopifnot(length(li) > 1)
l <- length(li)
res <- lapply(li[-1],function(X,x) identical(X,x),x=li[[1]])
res <- all(unlist(res))
res
}
compareList(test_true)
compareList(test_false)
Any suggestions? Are there any native checks for identical for more than just pairwise comparison?
有什么建议么?除了成对比较之外,是否存在相同的本机检查?
5 个解决方案