if statement - R - check whole vector in if clause -


here little example:

foo <- 1:5 foo < 6 > true true true true true if(foo < 6) {   # if(foo[1]<6 & foo[2]<6 & foo[3]<6 ...   # } 

and need if-claus if 3 elements true (order not important), e.g.

bar <- c(1,3,6,8,2) if(bar < 5) {   # if 3 values true    # } 

thx help.

you looking all().

> all(foo < 6) [1] true 

and second part, do

if (sum(bar < 5) == 3) {} 

this works because logical vector coerce numeric 0 , 1.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -