[R] subsetting

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jan 4 20:05:17 CET 2008


Here are three ways:

1. Define inner inside outer:

outer <- function() { s <- 1; inner <- function() s; inner() }
outer() # 1

2.  Use parent.frame:

inner <- function() parent.frame()$s
outer <- function() { s <- 1; inner() }
outer() # 1

3. Pass s explicitly:

inner <- function(s) s
outer <- function() { s <- 1; inner(s) }
outer() # 1



On Jan 4, 2008 1:36 PM, Matthias Wendel <office at matthiaswendel.de> wrote:
>
> 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