I am looking for an elegant way how I can test the equality of lengths of multiple vectors. For example, this is working fine:> length(rnorm(4)) == length(rnorm(5))[1] FALSE However this is not:> length(rnorm(4)) == length(rnorm(5)) == length(rnorm(6))Error: unexpected '==' in "length(rnorm(4)) == length(rnorm(5)) ==" Ofcourse I can test it by taking pairwise vector i.e. (1,2), (1,3), (2,3) and conclude all are of same length only if all 3 tests are passed. Is there any better (R) way to doing that? Thanks in advance [[alternative HTML version deleted]]
Henrique Dallazuanna
2011-Jan-31 19:25 UTC
[R] Comparing lengths of different vectors simultaneously
Try this: length(unique(sapply(list(rnorm(4), rnorm(5), rnorm(6)), length))) == 1 On Mon, Jan 31, 2011 at 5:14 PM, Megh Dal <megh700004@gmail.com> wrote:> I am looking for an elegant way how I can test the equality of lengths of > multiple vectors. > > For example, this is working fine: > > > length(rnorm(4)) == length(rnorm(5)) > [1] FALSE > However this is not: > > > length(rnorm(4)) == length(rnorm(5)) == length(rnorm(6)) > Error: unexpected '==' in "length(rnorm(4)) == length(rnorm(5)) ==" > Ofcourse I can test it by taking pairwise vector i.e. (1,2), (1,3), (2,3) > and conclude all are of same length only if all 3 tests are passed. Is > there > any better (R) way to doing that? > > Thanks in advance > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]