Skip to content

Including a vector element in an if statement

3 messages · Enda Hargaden, jim holtman, David Winsemius

#
If all you want is a count, then you can construct a logical vector
and count the TRUEs:

sum(a9 < (mean(a9) / 2))
On Sun, Apr 19, 2009 at 5:58 PM, Enda Hargaden <endahargaden at gmail.com> wrote:

  
    
#
On Apr 19, 2009, at 5:58 PM, Enda Hargaden wrote:

            
See below. the 'if' control construct is not set up to accept vectors.
 From the help page ?"if"
if(cond) expr
Arguments
cond 	A length-one logical vector that is not NA.
Jim gave you a perfectly workable solution, but you might find it  
instructive to review the difference between "if" and the function  
"ifelse". Generally "if" will not be useful in situations where you  
are doing comparisons on indexed objects whereas that is precisely  
what "ifelse" is designed to do. (Tthis was also pointed out in a  
response to a question earlier today.)


David Winsemius, MD
Heritage Laboratories
West Hartford, CT