mtb954 at gmail.com
2013-Jan-18 23:17 UTC
[R] Object created within a function disappears after the function is run
Dear R-helpers, I have run the code below which I expected to make an object called dd1, but that object does not exist. So, in summary, my problem is that my function is meant to make an object (dd1), and it does indeed make that object (I know that the last line of the function prints it out) but then, after the function has run, the object has disappeared. It's late on a Friday so I may be overlooking something obvious, but I'd appreciate your help if you can see what I'm doing wrong. Many thanks, Mark Na rm(list=ls()) #LOAD package, data, and keep clean copies of data library(reshape2); library(MuMIn);library(AICcmodavg) birdmatrix<-read.csv("birdmatrix.csv",stringsAsFactors=FALSE); birdmatrixclean<-birdmatrix habitatmatrix<-read.csv("habitatmatrix.csv",stringsAsFactors=FALSE); habitatmatrixclean<-habitatmatrix traits<-read.csv("traits.csv",stringsAsFactors=FALSE); traitsclean<-traits birdhabitat<-merge(birdmatrix,habitatmatrix,by="SARBASINYEAR") #merge bird and habitat data data<-birdhabitat; data$SAR<-substr(data$SARBASINYEAR,1,3) #convenience; create SAR variable #make a FUNCTION to combine variables into models dredgeit<-function(lm1){ dd1<-dredge(lm1,eval=FALSE) #create all possible combinations dd1<-gsub("formula = ","",dd1,fixed=TRUE) #delete characters dd1<-gsub(" + 1","",dd1,fixed=TRUE) #delete characters dd1#inspect model formulae } lm1<-lm(data$BiRich.o~log(data$HaArea,10)+log(data$HaPeri,10)) #saturated submodel dredgeit(lm1) [[alternative HTML version deleted]]
mtb954 at gmail.com
2013-Jan-18 23:25 UTC
[R] Object created within a function disappears after the function is run
Hi Sarah, that works perfectly, thank you! Mark Na On Fri, Jan 18, 2013 at 5:23 PM, Sarah Goslee <sarah.goslee@gmail.com>wrote:> Hi, > > You need to assign the result of the function to an object: > > dd1 <- dredgeit(lm1) > > On Fri, Jan 18, 2013 at 6:17 PM, <mtb954@gmail.com> wrote: > > Dear R-helpers, > > > > I have run the code below which I expected to make an object called dd1, > > but that object does not exist. > > > > So, in summary, my problem is that my function is meant to make an object > > (dd1), and it does indeed make that object (I know that the last line of > > the function prints it out) but then, after the function has run, the > > object has disappeared. > > > > It's late on a Friday so I may be overlooking something obvious, but I'd > > appreciate your help if you can see what I'm doing wrong. > > > > Many thanks, > > > > Mark Na > > > > > > > > rm(list=ls()) > > > > #LOAD package, data, and keep clean copies of data > > library(reshape2); library(MuMIn);library(AICcmodavg) > > birdmatrix<-read.csv("birdmatrix.csv",stringsAsFactors=FALSE); > > birdmatrixclean<-birdmatrix > > habitatmatrix<-read.csv("habitatmatrix.csv",stringsAsFactors=FALSE); > > habitatmatrixclean<-habitatmatrix > > traits<-read.csv("traits.csv",stringsAsFactors=FALSE); > traitsclean<-traits > > birdhabitat<-merge(birdmatrix,habitatmatrix,by="SARBASINYEAR") #merge > bird > > and habitat data > > data<-birdhabitat; data$SAR<-substr(data$SARBASINYEAR,1,3) #convenience; > > create SAR variable > > > > #make a FUNCTION to combine variables into models > > dredgeit<-function(lm1){ > > dd1<-dredge(lm1,eval=FALSE) #create all possible combinations > > dd1<-gsub("formula = ","",dd1,fixed=TRUE) #delete characters > > dd1<-gsub(" + 1","",dd1,fixed=TRUE) #delete characters > > dd1#inspect model formulae > > } > > > > lm1<-lm(data$BiRich.o~log(data$HaArea,10)+log(data$HaPeri,10)) #saturated > > submodel > > dredgeit(lm1) > > > -- > Sarah Goslee > http://www.functionaldiversity.org >[[alternative HTML version deleted]]