Old substitute trick:
a<-data.frame(x=1:10,y=1:10) test<-function(z){ mean.x<-mean(z$x) nm <-deparse(substitute(z)) print(nm) return(mean.x)} test(a)
Change: run it with a new test object
Note: this will not be done inside the local function when the set of list items is passed from the first argument to lapply (and also fails when the object is passed from the list passed for for -loop.) You can extract the ".Names" -attribute and order processing from the result of the structure, if it was a named vector that was being processed.
> lapply( list(a=4,b=5), function(x) {nm <- deparse(substitute(x)); strsplit(nm, '\\[')} ) $a $a[[1]] [1] "X" "" "1L]]" $b $b[[1]] [1] "X" "" "2L]]" > lapply( c(a=4,b=5), function(x) {nm <- deparse(substitute(x)); strsplit(nm, '\\[')} ) $a $a[[1]] [1] "structure(c(4, 5), .Names = c(\"a\", \"b\"))" "" [3] "1L]]" $b $b[[1]] [1] "structure(c(4, 5), .Names = c(\"a\", \"b\"))" "" [3] "2L]]"
May 42-09 '12 at 17:09 2012-05-09 17:09
source share