0

I would like a way to tell if 3 or more lists have a length equal to another variable. I can only find methods that work with 2 entries.

I have tried "==", all.equal and identical; neither of which work. A small example is shown below, I would like to have some TEST fcn that returns TRUE to TEST(x,A,B,C) and FALSE to TEST(x,A,B,D) (or even just not TRUE will do).

A small example is shown below - apologies for the appalling formatting but was the only way I could get this to post

E.g.

x = 3

A = length(c(1,2,3))
B = length(c(4,5,7))
C = length(c(2,4,6))
D = length(c(1,2))


x==A
*[1] TRUE*

c==A==C
*Error: unexpected '==' in "c==A=="*

all.equal(x,A)
*[1] TRUE*

all.equal(x,D)
*[1] "Mean relative difference: 0.3333333"*

all.equal(x,A,D)
*[1] TRUE*

identical(x,B)
*[1] FALSE*

identical(x,D)
*[1] FALSE*

identical(A,B)
*[1] TRUE*

identical(A,D)
*[1] FALSE*
thelatemail
  • 91,185
  • 12
  • 128
  • 188
Esme_
  • 1,360
  • 3
  • 18
  • 30
  • 1
    Might be of use to you: [test for equality among all elements of a single vector](http://stackoverflow.com/questions/4752275/test-for-equality-among-all-elements-of-a-single-vector) – thelatemail Oct 22 '14 at 00:01
  • 2
    What if you did `sapply(list(A,B,C,D), "==", x)` Of course, that's just the same as `c(A,B,C,D) %in% x` – Rich Scriven Oct 22 '14 at 00:05
  • 1
    @RichardScriven - function your code snippet: `compare.all <- function(x,compare) { all(sapply(compare, "==", x)) }` - then `compare.all(x, list(A,B,D) )` seems to work. – thelatemail Oct 22 '14 at 00:08
  • Thanks RichardScriven and thelatemail that gives me the result I was looking for. – Esme_ Oct 27 '14 at 22:05

0 Answers0