Skip to main content
changed `eval(parse(...))` back to `assign`
Source Link
Ricardo Saporta
  • 55.5k
  • 17
  • 149
  • 180

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.

 eval(parse assign(text="i<-i+1")"i", i+1, envir=parent.frame(n=2)) eval(parse assign(text="z<-z+1")"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 eval(parseassign(text="i<-i+1")"i", i+1, envir=parent.frame(2)) eval(parseassign(text="z<-z+1")"z", z+1, envir=parent.frame(3)) }) cat(sprintf("i=%d\n", i)) i } 

OUTPUT

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

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.

 eval(parse(text="i<-i+1"), envir=parent.frame(n=2)) eval(parse(text="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 eval(parse(text="i<-i+1"), envir=parent.frame(2)) eval(parse(text="z<-z+1"), envir=parent.frame(3)) }) cat(sprintf("i=%d\n", i)) i } 

OUTPUT

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

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=100 age=12, weight=105 age=13, weight=110 i=3 > cat(sprintf("y=%d, z=%d\n", y, z)) y=3, z=3 
deleted 6 characters in body
Source Link
Ricardo Saporta
  • 55.5k
  • 17
  • 149
  • 180

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.

 eval(parse(text="i<-i+1"), envir=parent.frame(n=2)) eval(parse(text="z<-z+1"), envir=parent.frame(n=3)) 


testme <- function(df, z) { 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 eval(parse(text="i<-i+1"), envir=parent.frame(2)) eval(parse(text="z<-z+1"), envir=parent.frame(3)) }) cat(sprintf("i=%d\n", i)) i } 

OUTPUT

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

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.

 eval(parse(text="i<-i+1"), envir=parent.frame(n=2)) eval(parse(text="z<-z+1"), envir=parent.frame(n=3)) 


testme <- function(df, z) { 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 eval(parse(text="i<-i+1"), envir=parent.frame(2)) eval(parse(text="z<-z+1"), envir=parent.frame(3)) }) cat(sprintf("i=%d\n", i)) i } 

OUTPUT

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

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.

 eval(parse(text="i<-i+1"), envir=parent.frame(n=2)) eval(parse(text="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 eval(parse(text="i<-i+1"), envir=parent.frame(2)) eval(parse(text="z<-z+1"), envir=parent.frame(3)) }) cat(sprintf("i=%d\n", i)) i } 

OUTPUT

> z <- 0 > y <- testme(x) age=11, weight=100 age=12, weight=105 age=13, weight=110 i=3 > cat(sprintf("y=%d, z=%d\n", y, z)) y=3, z=3 
added 129 characters in body
Source Link
Ricardo Saporta
  • 55.5k
  • 17
  • 149
  • 180

try the following ininside your apply. Mess around Experiment with the value of posn. I believe that for i it should be one less than for z.

 eval(parse(text="i<-i+1"), envir=parent.frame(n=2))  assigneval("i"parse(text="z<-z+1"), i+1envir=parent.frame(n=3)) 


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

OUTPUT

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

try the following in your apply. Mess around with the value of pos

 assign("i", i+1, pos=2) assign("z", z+1, pos=2) 

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.

 eval(parse(text="i<-i+1"), envir=parent.frame(n=2))  eval(parse(text="z<-z+1"), envir=parent.frame(n=3)) 


testme <- function(df, z) {  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 eval(parse(text="i<-i+1"), envir=parent.frame(2)) eval(parse(text="z<-z+1"), envir=parent.frame(3)) }) cat(sprintf("i=%d\n", i)) i } 

OUTPUT

> z <- 0 > y <- testme(x, z) age=11, weight=100 age=12, weight=105 age=13, weight=110 i=3 > cat(sprintf("y=%d, z=%d\n", y, z)) y=3, z=3  
Source Link
Ricardo Saporta
  • 55.5k
  • 17
  • 149
  • 180
Loading