Short answer, environments -- ls() looks (by default) in its current environment, which is not the same as the global environment when being called inside a function.
This would (I think) give the same answer but I haven't checked it. : > allobj<-function(){ + xyz<-as.vector(c(ls(.GlobalEnv),as.matrix(lapply(ls(),class)))); + dim(xyz)<-c(length(xyz)/2,2); + return(xyz) + } On Mon, Feb 20, 2012 at 10:07 AM, Ajay Askoolum <aa2e...@yahoo.co.uk> wrote: > This is copy & paste from my session: > >> xyz<-as.vector(c(ls(),as.matrix(lapply(ls(),class)))) >> dim(xyz)<-c(length(xyz)/2,2) >> >> allobj<-function(){ > + xyz<-as.vector(c(ls(),as.matrix(lapply(ls(),class)))); > + dim(xyz)<-c(length(xyz)/2,2); > + return(xyz) > + } >> xyz > [,1] [,2] > [1,] "a" "character" > [2,] "aa" "character" > [3,] "abc" "character" > [4,] "AirPassengers" "character" > [5,] "allobj" "character" > [6,] "allObjects" "character" > [7,] "allObjects2" "character" > [8,] "arrayFromAPL" "character" > [9,] "classes" "character" > [10,] "myCharVector" "character" > [11,] "myDateVector" "character" > [12,] "myNumericVector" "character" > [13,] "newArrayFromAPL" "character" > [14,] "obj" "character" > [15,] "objClass" "character" > [16,] "x" "character" > [17,] "xyz" "character" > [18,] "y" "character" >> allobj() > [,1] [,2] >> > > As far as I can see, the function allobj has the same expressions as those > executed from the command line. Why are the results different? > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > ______________________________________________ R-help@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.