On Nov 10, 2010, at 3:47 AM, Nick Sabbe wrote:

Hi Nikos.

There is quite a bit going on here, both in the code and in your
terminology.
You should really consider reading "An introduction to R" that comes with
your R installation.

A few pointers though:
* in R speak, you have nowhere "declared 2 global matrices": it is not
completely clear why you use code like "y<-c(NA)" to try to achieve such a thing, but if I'm not mistaken, this creates a logical vector of length 1.
Surely not a matrix.
* operator <- only looks for variables in the environment in which they are
evaluated,

Not really true. Looks first in the local environment but if not present, looks successively in the enclosing environments.

as does = (note: I would advise you to use <- in R as an
assignment operator instead of =). If you want to change variables in other environments, particularly the global environment, you need to use <<- (?<<-
does not seem to work to get you to its help page,

Use:
> ?"<<-"


but open R help, then
find the search page and search for <<-, for more information).
* apart from that: you may want to avoid the for loop here altogether:
y[i:10]<-(i:10)+1
f[i:10]<-y[(i-1):9]/2
gives you the same result, but more in the R fashion (in general, you want
to avoid explicit for loops in R)

HTH,


Nick Sabbe
--
ping: nick.sa...@ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36

-- Do Not Disapprove




-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org ] On
Behalf Of rnick
Sent: woensdag 10 november 2010 7:51
To: r-help@r-project.org
Subject: [R] Simple Function


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) other

Please check my code 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-tp3035572p3035572.html
Sent from the R help mailing list archive at Nabble.com.

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to