save objects of own function to workspace
On Feb 14, 2012, at 9:20 AM, Marion Wenty wrote:
Dear R-helpers, I created an own function which looks like this s_elternmz <- function(Var="balt")
{
Dg_a<-mz[,c("asbhh","apkz",Var)]
colnames(Dg_a)[colnames(Dg_a)=="apkz"]<-"bpkzm"
colnames(Dg_a)[colnames(Dg_a)==Var]<-paste(Var,"_m",sep="")
mz_int<-merge(mz,Dg_a,by=c("asbhh","bpkzm"),all.x=T)
colnames(Dg_a)[colnames(Dg_a)=="bpkzm"]<-"bpkzv"
colnames(Dg_a)[colnames(Dg_a)==paste(Var,"_m",sep="")]<-
paste(Var,"_v",sep="")
Why take two steps to change that column name?
mz_int<-merge(mz_int,Dg_a,by=c("asbhh","bpkzv"),all.x=T)
You just overwrote an object that you created earklier but have not done anything with in the meantime.
mz_int <- mz_int[order(mz_int$asbhh,mz_int$apkz), c(colnames(mz),paste(Var,"_m",sep=""), paste(Var,"_v",sep=""))] } My problem is that the objects are not saved in the workspace. Especially I need the object which I created in the end - mz_int - because I want to do calculations with it in R afterwards.
Then just assign it: mz_result <- s_elternmz() As you have noticed, assignment inside a function is not a durable event. The function returns the value of either its first encounter with return() or the value of hte last assignment (but not the name to which that object was assigned.) Welcome to R's version of functional programming.
Thank you very much for your help in advance. Marion [[alternative HTML version deleted]]
______________________________________________ R-help at 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.
David Winsemius, MD West Hartford, CT