[R] programing problem with for( )
     (Ted Harding) 
    Ted.Harding at manchester.ac.uk
       
    Mon Feb  1 12:41:48 CET 2010
    
    
  
On 01-Feb-10 11:29:40, marlene marchena wrote:
> Hi R-users
> 
> I'm writing a code to run a fuction but I found an error that I can't
> fix. I
> reproduced the error with a simple example.
> 
> The correct answer is k but I can't fill my s matrix. What I'm doing
> wrong?
> 
> 
>  s<-matrix(data=NA,nrow=1,ncol=5 )
>  s
> 
> for(i in 1:5)
>       {
>       k=sqrt(i)
>       s[,i]<-k[i]
>    print(k)
>       }
> 
> s
> 
> Thanks in advance,
> Marlene.
Use s[,i]<-k instead of s[,i]<-k[i] since k=sqrt(i) assigns a
single value to k. Hence k[1] will be the same as k, but for
i>1 k[i] will always be NA.
A better way to do the whole thing (in your example) is simply
  s <- sqrt(1:5)
but your example may be a very simple case of a more complex
procedure!
Hoping this helps,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 01-Feb-10                                       Time: 11:41:46
------------------------------ XFMail ------------------------------
    
    
More information about the R-help
mailing list