[R] subsetting

Bert Gunter gunter.berton at gene.com
Fri Jan 4 19:50:12 CET 2008


See FAQ 7.12 and the R Language Definition manual on scoping.

R uses lexical scoping which means here that s does not exist in the
enclosing environment of inner(), which is the global environment. 


Bert Gunter
Genentech Nonclinical Statistics
7-7374

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Matthias Wendel
Sent: Friday, January 04, 2008 10:37 AM
To: r-help at r-project.org
Subject: [R] subsetting


I'm using R Version 2.6.1 under Windows XP.

> search()
 [1] ".GlobalEnv"            "s"                     "s"
"package:cairoDevice"  
 [5] "package:datasets"      "package:foreign"       "package:graphics"
"package:grDevices"    
 [9] "package:gWidgetsrJava" "package:gWidgets"      "package:Hmisc"
"package:JGR"          
[13] "package:JavaGD"        "package:rJava"         "package:stats"
"package:utils"        
[17] "package:methods"       "Autoloads"             "package:base"  

The Problem: I'm using a function which is constructing a subset of a
dataframe. This dataframe is used in another function. The
structure is like this:

> inner = function (){
+ print('inner:')
+ print(s)
+ }
> outer = function(){
+ t = data.frame(list(X=1:10, Y=LETTERS[1:10]))
+ s = t[t[,'X'] < 5, ]
+ print('outer:')
+ print(t[, 'Y'])
+ inner()
+ }
> outer()

And the response in the R-Console is:

[1] "outer:"
 [1] A B C D E F G H I J
Levels: A B C D E F G H I J
[1] "inner:"
Fehler in print(s) : objekt "s" nicht gefunden
(Error in print(s) : object "s" not found)

How can I make s reachable in inner?

Regards,
Matthias

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




More information about the R-help mailing list