[R] Strange problem.

Spencer Graves spencer.graves at pdf.com
Tue Sep 2 03:54:06 CEST 2003


	  Note also the warning:  "the condition has length > 1 and only the 
first element will be used in: if (n < 9543) 0 else prior(n)/n".  The 
problem is that "if" is not vectorized:  "if(n<9543)" is interpreted as 
"if(n[1]<9543)", which then produces a scalar output, 0, which 
presumably then generates the error message "x and y lengths differ".

	  Try "ifelse" instead, so "post" becomes:

post<-function(n){
  ifelse(n < 9543, 0, prior(n)/n)
  }

	  Then plot(post,1,15000) seemed to work fine for me.

hope this helps.  spencer graves

Yannong.Dong-1 wrote:
> Hi, everyone,
>     I am a new user of R. Recently, I tried the following commands, but couldn't make them work. If any one of you has some ideas about it, please help me. The commands are
> 
> 
>>std<-1000
>>mean<-8000
>>prior<-function(n){1/(sqrt(2*pi)*std)*exp(-1.0*(n-mean)^2/(2*std^2))}
>>plot(prior,1,15000)
>>post<-function(n){
> 
> + if(n < 9543) 
> +  0
> + else
> +  prior(n)/n
> + }
> 
>>plot(post,1,15000)  # This command didn't work. The error message is " x and y lengths differ "
> 
> 
> I was really confused about it because the first function "prior" can work well. I think I must make something wrong stupidly, but could not find it. If you can help me about it, it would be very helpful. Thanx a lot in advance.
> 
>    Rgds, 
>    Yannong Dong
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list