[R] Local Variable

2010-09-16 Thread harunpirim

Dear All,

I have a function:

gen - function(m){for(i in 1:m){assign(paste(x,i, sep=),
(7*get(paste(x,i-1,sep=)))%%13); print(get(paste(x, i, sep=)))}}

my question is, I do gen(20), then the function prints out 20 values,
however when I do objects(), I can't see x1,x2,x3,,x20 as objects. Why?
Are they local variables? R doesn't show local variables as objects?

Thanks,

Harun
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Local-Variable-tp2542441p2542441.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Local Variable

2010-09-16 Thread Sarah Goslee
Leaving aside the question of whether this is a good thing to do, you
can accomplish what you want by changing the pos argument, which
by default does not save the variable to the global environment.

Reading the details section of ?assign and also ?environment may help.

I'd offer an example, but your function doesn't work as-is because I don't
have any idea what the get statements are supposed to be getting.

Sarah

On Thu, Sep 16, 2010 at 11:36 AM, harunpirim harunpi...@gmail.com wrote:

 Dear All,

 I have a function:

 gen - function(m){for(i in 1:m){assign(paste(x,i, sep=),
 (7*get(paste(x,i-1,sep=)))%%13); print(get(paste(x, i, sep=)))}}

 my question is, I do gen(20), then the function prints out 20 values,
 however when I do objects(), I can't see x1,x2,x3,,x20 as objects. Why?
 Are they local variables? R doesn't show local variables as objects?

 Thanks,

 Harun

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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.


Re: [R] Local Variable

2010-09-16 Thread Erik Iverson



Sarah Goslee wrote:
Leaving aside the question of whether this is a good thing to do, 


Let's not leave that aside.  Surely there is a much more
straight-forward way to accomplish what you want.  Why not store
all the X's in a vector, and then you can avoid this for/assign/get
hack.


you

can accomplish what you want by changing the pos argument, which
by default does not save the variable to the global environment.

Reading the details section of ?assign and also ?environment may help.

I'd offer an example, but your function doesn't work as-is because I don't
have any idea what the get statements are supposed to be getting.

Sarah

On Thu, Sep 16, 2010 at 11:36 AM, harunpirim harunpi...@gmail.com wrote:

Dear All,

I have a function:

gen - function(m){for(i in 1:m){assign(paste(x,i, sep=),
(7*get(paste(x,i-1,sep=)))%%13); print(get(paste(x, i, sep=)))}}

my question is, I do gen(20), then the function prints out 20 values,
however when I do objects(), I can't see x1,x2,x3,,x20 as objects. Why?
Are they local variables? R doesn't show local variables as objects?

Thanks,

Harun




__
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.