I could not find any help pages on How to test many objects for being of equal length Something like identical for more than two objects? x<-1:6 y<-1:10 z<-3:5 ## For two objects I can do: identical(length(x),length(y)) ## For more than two I currently can do: length(unique(c(length(x),length(y),length(z))))==1 but there must be a better way. Thanks, M
Hi Manuel, First, encapsulate yoyr objects within a list. That will help you manipulate all them at once and ensures that the final function will work with whatever number of vectors. ?? ll <- list(x,y,z) ?? sapply(ll,length) [1] 6 10 3 Then you can use your length(unique(...))==1 Another way is to use all: ?? all(sapply(ll,length)==length(ll[[1]])) [1] FALSE HTH, Eric At 09:09 9/12/2004, Manuel Gutierrez wrote:>I could not find any help pages on How to test many >objects for being of equal length >Something like identical for more than two objects? >x<-1:6 >y<-1:10 >z<-3:5 >## For two objects I can do: >identical(length(x),length(y)) >## For more than two I currently can do: >length(unique(c(length(x),length(y),length(z))))==1 > >but there must be a better way. >Thanks, >M > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlEric Lecoutre UCL / Institut de Statistique Voie du Roman Pays, 20 1348 Louvain-la-Neuve Belgium tel: (+32)(0)10473050 lecoutre at stat.ucl.ac.be http://www.stat.ucl.ac.be/ISpersonnel/lecoutre If the statistics are boring, then you've got the wrong numbers. -Edward Tufte
BXC (Bendix Carstensen)
2004-Dec-09 08:30 UTC
[R] test multiple objects for being equal length
not that its much shorter: length( table( sapply( list(x,y,z), length ) ) ) == 1 Bendix ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 30 75 87 38 fax: +45 44 43 07 06 bxc at steno.dk www.biostat.ku.dk/~bxc ----------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Manuel > Gutierrez > Sent: Thursday, December 09, 2004 9:09 AM > To: r-help at stat.math.ethz.ch > Subject: [R] test multiple objects for being equal length > > > I could not find any help pages on How to test many > objects for being of equal length > Something like identical for more than two objects? > x<-1:6 > y<-1:10 > z<-3:5 > ## For two objects I can do: > identical(length(x),length(y)) > ## For more than two I currently can do: > length(unique(c(length(x),length(y),length(z))))==1 > > but there must be a better way. > Thanks, > M > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read > the posting guide! http://www.R-project.org/posting-guide.html >
Wrapping the suggestions into a function:> sameLength <- function(...) {+ n <- sapply(list(...), length) + all(n == n[1]) + }> sameLength(double(1), double(2))[1] FALSE> sameLength(double(1), double(1), list(x=1))[1] TRUE [Note that if you have lots of objects to compare, length(unique(...))==1 will not be as efficient.] HTH, Andy> From: Manuel Gutierrez > > I could not find any help pages on How to test many > objects for being of equal length > Something like identical for more than two objects? > x<-1:6 > y<-1:10 > z<-3:5 > ## For two objects I can do: > identical(length(x),length(y)) > ## For more than two I currently can do: > length(unique(c(length(x),length(y),length(z))))==1 > > but there must be a better way. > Thanks, > M > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >