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