Re: [R] How to make the assignment in a for-loop not affect variables outside the loop?

2009-12-10 Thread John Fox
Try

n=10
b=1
local(
for(i in 1:n) {
  n=3
  print(n)
  b <<- b*i
}
)

print(n)
print(b)

or

n=10
b=1
local(
for(i in 1:n) {
  n=3
  print(n)
  assign("b", b*i, env=.GlobalEnv)
}
)

print(n)
print(b)

John

> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On
> Behalf Of Peng Yu
> Sent: December-09-09 11:03 PM
> To: r-h...@stat.math.ethz.ch
> Subject: Re: [R] How to make the assignment in a for-loop not affect
> variables outside the loop?
> 
> 2009/11/22 Uwe Ligges :
> > Either use local as in:
> >
> > n=10
> >
> > local(for(i in 1:n){
> >     n=3
> >     print(n)
> > })
> >
> > print(n)
> 
> 'local()' makes everything inside it unavailable outside of it. Is
> there a way to make 'n' unavailable outside but still make 'b'
> available outside without using 'function'?
> 
> n=10
> b=1
> local(
> for(i in 1:n) {
>   n=3
>   print(n)
>   b=b*i
> }
> )
> 
> print(n)
> print(b)
> 
> 
> > or write a function that is evaluated in its own environment:
> >
> > n=10
> >
> > MyLoopFoo <- function(){
> >    for(i in 1:n){
> >        n <- 3
> >        print(n)
> >    }
> > }
> >
> > MyLoopFoo()
> >
> > print(n)
> >
> >
> >
> >
> > Uwe Ligges
> >
> >
> > Peng Yu wrote:
> >>
> >> I know that R is a dynamic programming language. But I'm wondering if
> >> there is a way to make the assignment in a for-loop not affect
> >> variables outside the loop.
> >>
> >>> n=10
> >>> for(i in 1:n){
> >>
> >> +     n=3
> >> +     print(n)
> >> + }
> >> [1] 3
> >> [1] 3
> >> [1] 3
> >> [1] 3
> >> [1] 3
> >> [1] 3
> >> [1] 3
> >> [1] 3
> >> [1] 3
> >> [1] 3
> >>>
> >>> print(n)
> >>
> >> [1] 3
> >>
> >> __
> >> 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.


Re: [R] How to make the assignment in a for-loop not affect variables outside the loop?

2009-12-09 Thread Peng Yu
2009/11/22 Uwe Ligges :
> Either use local as in:
>
> n=10
>
> local(for(i in 1:n){
>     n=3
>     print(n)
> })
>
> print(n)

'local()' makes everything inside it unavailable outside of it. Is
there a way to make 'n' unavailable outside but still make 'b'
available outside without using 'function'?

n=10
b=1
local(
for(i in 1:n) {
  n=3
  print(n)
  b=b*i
}
)

print(n)
print(b)


> or write a function that is evaluated in its own environment:
>
> n=10
>
> MyLoopFoo <- function(){
>    for(i in 1:n){
>        n <- 3
>        print(n)
>    }
> }
>
> MyLoopFoo()
>
> print(n)
>
>
>
>
> Uwe Ligges
>
>
> Peng Yu wrote:
>>
>> I know that R is a dynamic programming language. But I'm wondering if
>> there is a way to make the assignment in a for-loop not affect
>> variables outside the loop.
>>
>>> n=10
>>> for(i in 1:n){
>>
>> +     n=3
>> +     print(n)
>> + }
>> [1] 3
>> [1] 3
>> [1] 3
>> [1] 3
>> [1] 3
>> [1] 3
>> [1] 3
>> [1] 3
>> [1] 3
>> [1] 3
>>>
>>> print(n)
>>
>> [1] 3
>>
>> __
>> 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.


Re: [R] How to make the assignment in a for-loop not affect variables outside the loop?

2009-11-22 Thread Uwe Ligges

Either use local as in:

n=10

local(for(i in 1:n){
 n=3
 print(n)
})

print(n)


or write a function that is evaluated in its own environment:

n=10

MyLoopFoo <- function(){
for(i in 1:n){
n <- 3
print(n)
}
}

MyLoopFoo()

print(n)




Uwe Ligges


Peng Yu wrote:

I know that R is a dynamic programming language. But I'm wondering if
there is a way to make the assignment in a for-loop not affect
variables outside the loop.


n=10
for(i in 1:n){

+ n=3
+ print(n)
+ }
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3

print(n)

[1] 3

__
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] How to make the assignment in a for-loop not affect variables outside the loop?

2009-11-22 Thread Peng Yu
I know that R is a dynamic programming language. But I'm wondering if
there is a way to make the assignment in a for-loop not affect
variables outside the loop.

> n=10
> for(i in 1:n){
+ n=3
+ print(n)
+ }
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
[1] 3
>
> print(n)
[1] 3
>

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