Is there some fast (built-in?) way to get the OR of all the elements in a logical vector? In other words, is there some fast (built-in) version of the function vor below? Thanks. -Ben vor <- function(v) { ans <- v[1] if (length(v) > 1) for (i in 2:length(v)) ans <- ans | v[i] ans }
Hi Ben, Always do consider that boolean vectors TRUE/FALSE are equivalent to integers 1/0. What you want is to know wether one element of a vector is TRUE, which is: > sum(vec)>0 HTH, Eric At 18:38 5/08/2004, Ben Wittner wrote:>Is there some fast (built-in?) way to get the OR of all the elements in a >logical vector? > >In other words, is there some fast (built-in) version of the function vor >below? > >Thanks. > >-Ben > >vor <- function(v) { > ans <- v[1] > if (length(v) > 1) > for (i in 2:length(v)) > ans <- ans | v[i] > ans >} > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.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
?any hope this helps. spencer graves Ben Wittner wrote:>Is there some fast (built-in?) way to get the OR of all the elements in a >logical vector? > >In other words, is there some fast (built-in) version of the function vor >below? > >Thanks. > >-Ben > >vor <- function(v) { > ans <- v[1] > if (length(v) > 1) > for (i in 2:length(v)) > ans <- ans | v[i] > ans >} > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
Ben Wittner <bwittner <at> jimmy.harvard.edu> writes: : Is there some fast (built-in?) way to get the OR of all the elements in a : logical vector? Here are two possibilities: max(x) == 1 sum(x) > 0 These use the fact that logicals used in arithmetic operations are converted such that TRUE becomes 1 and FALSE becomes 0.
Is there anything wrong with sum(v) > 0? Andy> From: Ben Wittner > > Is there some fast (built-in?) way to get the OR of all the > elements in a > logical vector? > > In other words, is there some fast (built-in) version of the > function vor > below? > > Thanks. > > -Ben > > vor <- function(v) { > ans <- v[1] > if (length(v) > 1) > for (i in 2:length(v)) > ans <- ans | v[i] > ans > } > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
> Is there some fast (built-in?) way to get the OR of all the elements in > a logical vector?any() and all() should give you OR and AND, respectively. Perhaps this should be in as a 'see also' for '|' and '&'. -J
I don't know how careful about coercing type you need to be. Something like TRUE %in% outer(v,v,"|") may work for you but simpler functions that do arithmetic and coerce the answer back to logical [e.g., as.logical(max(v))] might suffice.> xt <- c(T,T,T) > xf <- c(T,F,F) > outer(xt, xf, "|")[,1] [,2] [,3] [1,] TRUE TRUE TRUE [2,] TRUE TRUE TRUE [3,] TRUE TRUE TRUE> outer(xf, xf, "|")[,1] [,2] [,3] [1,] TRUE TRUE TRUE [2,] TRUE FALSE FALSE [3,] TRUE FALSE FALSE> TRUE %in% outer(xf, xf, "|")[1] TRUE> TRUE %in% outer(!xt, !xt, "|")[1] FALSE> #if you don't mind sloppiness > as.logical(max(xf))[1] TRUE> as.logical(max(!xt))[1] FALSE> as.logical(sum(xf^2))[1] TRUE> as.logical(sum((!xt)^2))[1] FALSE> #be careful > as.logical(max(c(-1,0,0))+ ) [1] FALSE>bob -----Original Message----- From: Ben Wittner [mailto:bwittner at jimmy.harvard.edu] Sent: Thursday, August 05, 2004 12:38 PM To: r-help at stat.math.ethz.ch Subject: [R] or of a logical vector Is there some fast (built-in?) way to get the OR of all the elements in a logical vector? In other words, is there some fast (built-in) version of the function vor below? Thanks. -Ben vor <- function(v) { ans <- v[1] if (length(v) > 1) for (i in 2:length(v)) ans <- ans | v[i] ans } ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On 05-Aug-04 Ben Wittner wrote:> Is there some fast (built-in?) way to get the OR of all the > elements in a logical vector? > > In other words, is there some fast (built-in) version of the function > vor below? > > Thanks. > > -Ben > > vor <- function(v) { > ans <- v[1] > if (length(v) > 1) > for (i in 2:length(v)) > ans <- ans | v[i] > ans > }It's a sort of cheating ("type-punning"), but so long as it's just the "or" you're after then sum(v)>0 will implement your 'vor', i.e. give you FALSE if all v[i]==FALSE, and TRUE if any v[i]==TRUE. And it's certainly fast. Similarly, prod(v)>0 would implement a 'vand'. But you can also do these with 'any' and 'all', e.g. any(v==TRUE) and all(v==TRUE) I'm not sure which of these two approaches would be faster, but I doubt there's much in it. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 167 1972 Date: 05-Aug-04 Time: 20:22:12 ------------------------------ XFMail ------------------------------
Yes, of course:> x <- c(TRUE, NA) > any(x)[1] TRUE> sum(x) > 0[1] NA Andy> From: Peter Dalgaard > > "Liaw, Andy" <andy_liaw at merck.com> writes: > > > Is there anything wrong with sum(v) > 0? > > Yes, there is an any()-thing.... ;-) > > (And NA handling differs too.) > > -- > O__ ---- Peter Dalgaard Blegdamsvej 3 > c/ /'_ --- Dept. of Biostatistics 2200 Cph. N > (*) \(*) -- University of Copenhagen Denmark Ph: > (+45) 35327918 > ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: > (+45) 35327907 > >
Maybe Matching Threads
- problem building dendrograms to use with heatmap()
- confidence interval too small in nlme?
- rgl.snapshot only captures a small portion what's visible in the RGL device window on CentOS 7
- anyNA() performance on vectors of POSIXct
- suggestions motivated by quest for remainders