I have an array:
a <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
and would like to implement the following function:
w<-function(a){
if (a>0){
a/sum(a)
}
else 1
}
This function would like to check whether there is any value in a
larger than 0 and if yes then divide each element by the sum of the total.
Otherwise it should just record 1.
I get the following warning message:
Warning message:
In if (a > 0) { :
the condition has length > 1 and only the first element will be used
How can I correct the function?
question from:
https://stackoverflow.com/questions/14170778/interpreting-condition-has-length-1-warning-from-if-function