[R] Simple Function

rnick nikos.rachmanis at gmail.com
Wed Nov 10 08:09:29 CET 2010


Hi guys, 

Very new to R and your help would be highly appreciated for the following
problem. I am trying to create a simple function which registers values
within an array through a for loop. These are the steps I have followed: 

1) Declared 2 global matrices 
2) Create function mat() with i as an input 
3) constructed the for loop 
4) called mat(2) 

The problem is that when i try to get y[4] and f[5] the output is: [1] NA 
  
my concern is that i am not addressing any of the following topics: 
1) definition of global variable 
2) the argument does not go through the for loop 
3) the matrices definition is not correct
4) the function do not return any values 
4) other.. 

My code is listed below: 

y=c(NA) 
f=c(NA) 
mat<-function(i) 
{ 
        for (k in i:10) 
        { 
                y[k]=k+1 
                f[k]=y[k-1]/2	
        } 
} 
mat(2) 

Any thoughts or recommendations would be highly appreciated. 

Thanks in advance, 

N
-- 
View this message in context: http://r.789695.n4.nabble.com/Simple-Function-tp3035585p3035585.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list