Scope of variable?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Scope of variable?

Ravi Kulkarni
I know this is a very elementary question... I could not find a solution looking at old posts.
I am unable to access a variable outside the scope of a for loop, even when the variable was defined before the loop:

haar <- function() {
a = c(1.4560773, 2.3752412, 0.9798882, 3.0909252, 2.3986487, 1.8581543)

for (i in c(1:100)) {
        m = matrix(rnorm(36)+1i*rnorm(36),6)
        qrm = qr(m)
        Q = qr.Q(qrm)
        eival = eigen(Q, only.values=T)
        ph = Arg(eival$values)
        v = Mod(ph)
        a = cat(a,v)
}
hist(a)
}

The hist command does not plot because a is null there.
How do I plot a? I tried using

                          assign("a", a, envir = .GlobalEnv)

but that does not help either... Neither did using <<-
Any help is most welcome.
  Ravi
Reply | Threaded
Open this post in threaded view
|

Re: Scope of variable?

Jonathan P Daily
?cat

cat prints text, and returns an invisible NULL. Also, it is general
practice to assign values using '<-' even inside of functions, for reasons
detailed in ?"<-"
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it."
     - Jubal Early, Firefly

[hidden email] wrote on 03/16/2011 07:41:36 AM:

> [image removed]
>
> [R] Scope of variable?
>
> Ravi Kulkarni
>
> to:
>
> r-help
>
> 03/16/2011 09:07 AM
>
> Sent by:
>
> [hidden email]
>
> I know this is a very elementary question... I could not find a solution
> looking at old posts.
> I am unable to access a variable outside the scope of a for loop, even
when

> the variable was defined before the loop:
>
> haar <- function() {
> a = c(1.4560773, 2.3752412, 0.9798882, 3.0909252, 2.3986487, 1.8581543)
>
> for (i in c(1:100)) {
>    m = matrix(rnorm(36)+1i*rnorm(36),6)
>    qrm = qr(m)
>    Q = qr.Q(qrm)
>    eival = eigen(Q, only.values=T)
>    ph = Arg(eival$values)
>    v = Mod(ph)
>    a = cat(a,v)
> }
> hist(a)
> }
>
> The hist command does not plot because a is null there.
> How do I plot a? I tried using
>
>                           assign("a", a, envir = .GlobalEnv)
>
> but that does not help either... Neither did using <<-
> Any help is most welcome.
>   Ravi
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Scope-
> of-variable-tp3381485p3381485.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Scope of variable?

Martyn Byng
In reply to this post by Ravi Kulkarni
Hi,

Not sure what you are trying to do with the "cat" command, but cat
returns an invisible NULL (as described in the doc), so

a = cat(a,v)

just sets a to NULL

Martyn

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
On Behalf Of Ravi Kulkarni
Sent: 16 March 2011 11:42
To: [hidden email]
Subject: [R] Scope of variable?

I know this is a very elementary question... I could not find a solution
looking at old posts.
I am unable to access a variable outside the scope of a for loop, even
when
the variable was defined before the loop:

haar <- function() {
a = c(1.4560773, 2.3752412, 0.9798882, 3.0909252, 2.3986487, 1.8581543)

for (i in c(1:100)) {
        m = matrix(rnorm(36)+1i*rnorm(36),6)
        qrm = qr(m)
        Q = qr.Q(qrm)
        eival = eigen(Q, only.values=T)
        ph = Arg(eival$values)
        v = Mod(ph)
        a = cat(a,v)
}
hist(a)
}

The hist command does not plot because a is null there.
How do I plot a? I tried using

                          assign("a", a, envir = .GlobalEnv)

but that does not help either... Neither did using <<-
Any help is most welcome.
  Ravi

--
View this message in context:
http://r.789695.n4.nabble.com/Scope-of-variable-tp3381485p3381485.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.

________________________________________________________________________
This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Scope of variable?

jholtman
In reply to this post by Ravi Kulkarni
I think you meant:

a <- c(a, v)

and not

a <- cat(a, v)


On Wed, Mar 16, 2011 at 7:41 AM, Ravi Kulkarni <[hidden email]> wrote:

> I know this is a very elementary question... I could not find a solution
> looking at old posts.
> I am unable to access a variable outside the scope of a for loop, even when
> the variable was defined before the loop:
>
> haar <- function() {
> a = c(1.4560773, 2.3752412, 0.9798882, 3.0909252, 2.3986487, 1.8581543)
>
> for (i in c(1:100)) {
>        m = matrix(rnorm(36)+1i*rnorm(36),6)
>        qrm = qr(m)
>        Q = qr.Q(qrm)
>        eival = eigen(Q, only.values=T)
>        ph = Arg(eival$values)
>        v = Mod(ph)
>        a = cat(a,v)
> }
> hist(a)
> }
>
> The hist command does not plot because a is null there.
> How do I plot a? I tried using
>
>                          assign("a", a, envir = .GlobalEnv)
>
> but that does not help either... Neither did using <<-
> Any help is most welcome.
>  Ravi
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Scope-of-variable-tp3381485p3381485.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Scope of variable?

Ravi Kulkarni
Of course! Works fine now.
Thanks, Martyn and Jim...

Ravi