how to access global/outer scope variable from R apply function? how to access global/outer scope variable from R apply function? r r

how to access global/outer scope variable from R apply function?


Using the <<- operator you can write to variables in outer scopes:

x = data.frame(age=c(11,12,13), weight=c(100,105,110))xtestme <- function(df) {    i <- 0    apply(df, 1, function(x) {        age <- x[1]        weight <- x[2]        cat(sprintf("age=%d, weight=%d\n", age, weight))        i <<- i+1   #this could not access the i variable in outer scope        z <<- z+1   #this could not access the global variable    })    cat(sprintf("i=%d\n", i))    i}z <- 0y <- testme(x)cat(sprintf("y=%d, z=%d\n", y, z))

The result here:

age=11, weight=100age=12, weight=105age=13, weight=110i=3y=3, z=3

Note that the usage of <<- is dangerous, as you break up scoping. Do this only if really necessary and if you do, document that behavior clearly (at least in bigger scripts)


try the following inside your apply. Experiment with the value of n. I believe that for i it should be one less than for z.

         assign("i", i+1, envir=parent.frame(n=2))         assign("z", z+1, envir=parent.frame(n=3))



testme <- function(df) {    i <- 0    apply(df, 1, function(x) {        age <- x[1]        weight <- x[2]        cat(sprintf("age=%d, weight=%d\n", age, weight))        ## ADDED THESE LINES         assign("i", i+1, envir=parent.frame(2))         assign("z", z+1, envir=parent.frame(3))    })    cat(sprintf("i=%d\n", i))    i}

OUTPUT

> z <- 0> y <- testme(x)age=11, weight=100age=12, weight=105age=13, weight=110i=3> cat(sprintf("y=%d, z=%d\n", y, z))y=3, z=3