On 02/10/2008 10:07 PM, Jason Lee wrote:> Hi,
>
> I came across the below error when I try to do ifelse condition:-
>
> Error in "[<-"(`*tmp*`, test, value = rep(yes, length =
length(ans))[test])
> :
> incompatible types
>
> What I am trying to accomplish is :-
>
> for(x in 1:ncol(filterpred)){
>
> sumno<-sum(filterpred[no,x])
> sumyes<-sum(filterpred[yes,x])
>
> ifelse(sumno==0 && sumyes !=0,
> filterpred[,x]<-NULL,filterpred[,x]<-filterpred[,x])
> }
>
> Anything wrong here?
You want to use if .. else .., not ifelse. ifelse() is a function that
takes a vector of logical values, and produces a vector of answers, not
a way to control program flow.
It almost never makes sense to use && in ifelse(), because &&
always
produces a scalar, not a vector. I think what you want is to replace
the ifelse() call with
if (sumno==0 && sumyes !=0) {
filterpred[,x]<-NULL
} else {
filterpred[,x]<-filterpred[,x])
}
Duncan Murdoch