[R] how to do a IF ELSE in a matrix format

Petr PIKAL petr.pikal at precheza.cz
Wed Aug 4 08:45:55 CEST 2010


Hi

r-help-bounces at r-project.org napsal dne 03.08.2010 19:02:41:

> question again.
> 
> here is part of my original code for my research question. originally I 
used a 
> loop with matrix form to do the work and it works (though looks ugly). I 
have 
> tried the methods mentioned in the above posts and thought I did the 
same thing. 
> but the new code does not work. the error message is:
> 
> Error in optim(guess, myfunc, data = mydata, method = "BFGS") : 
> initial value in 'vmmin' is not finite
> 
> which is the same error message if I have set up wrong numbers of 
estimated 
> parameters. but I have changed nothing but just this part. the command I 
use is 
> optim() with a self-writen function for maximum likelihood estimation.
> 
> 
> here is the part of my originall code. n is the number of observations:
> 
> for(j in 1:n) {
>     if (edu[j,i]==1) lia[j,i]=1/v8[j,i]
>     if (wrk[j,i]==1) lia[j,i]=exp(vbar2[j,i])/v8[j,i]
> }
> 
> and I have tried:
> lia[,i]<-ifelse(edu[,i]==1,1/v8[,i],0)
> lia[,i]<-ifelse(wrk[,i]==1,exp(vbar2[,i])/v8[,i],0)
> 
> any suggestions? thanks a lot for every of them.

Without reproducible example it is difficult. Did you try that both 
versions output into the same object? You probably compute vmmin, what is 
it after first code and what after the second.

If everything is in function try to use debug

?debug

Regards
Petr

> 
> Nan
> from Montreal
> 
> 
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list