I want to build this function: f(a,b)=sum_{i=1 to n}(max(a-b_i,0)) where b=(b_1,b_2,...b_n). This is what I have done:
vec<-function(a,b){
z<-0
for(i in b){
ifelse(a > i,z <- z + (a-i), 0)
}
return(data.frame(z))
}
This code gives correct output for scalar input of a. But while using vector output answers are not always correct. For example
> vec(c(-6,5),c(3,1,3))
gives -25 for a=-6 and 8 for a=5 respectively.
But > vec(-6,c(3,1,3)) gives 0. And this the correct answer.
Please throw give some idea how will I correct it.
Aucun commentaire:
Enregistrer un commentaire