An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120214/f98927e0/attachment.pl>
save objects of own function to workspace
8 messages · David Winsemius, Jeff Newmiller, Rolf Turner +1 more
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120214/12020107/attachment.pl>
It is possible to do what you ask using <<- instead of <-, but from the perspective of using the function it will be much clearer if you add mz as an argument to the function along with Var, and return mz_int at the end. (The easiest way to return a variable is to type it alone on the last line if your function.) Then you can assign the return value to some variable of your choosing (including possibly one named mz_int in the calling environment) where you call the function.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Marion Wenty <marion.wenty at gmail.com> 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="")
mz_int<-merge(mz_int,Dg_a,by=c("asbhh","bpkzv"),all.x=T)
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.
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.
On Feb 14, 2012, at 10:59 AM, Marion Wenty wrote:
Thank you very much for your help, David!
Now I have got an object which I can work with.
You mentioned that I took two steps to create the new column names.
I had tried doing it in one step but couldn't find out how. Could
you help me with that, as well?
Thank you very much!
Marion
2012/2/14 David Winsemius <dwinsemius at comcast.net>
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="")
The above line seemed to be the first change to make <Var>_m
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="")
Then this line changed <Var>_m to become <Var>_v. Why not change it to <Var>_v in the first place?
Why take two steps to change that column name?
David Winsemius, MD West Hartford, CT
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120215/a7aa7272/attachment.pl>
On 16/02/12 03:59, Marion Wenty wrote:
<SNIP>
Thank you for your answers! Jeff, thanks very much for the tip with the<<- instead of using<- to save the objects outside of the function, as well! I find this very usefull and convenient, also if I decide to choose more objects.
<SNIP>
This practice is to be *STRONGLY* discouraged. If you persist,
you will live to regret it.
Write functions that return (arbitrarily complicated) objects and
assign the output of functions to objects. DO NOT write functions
that mess around with your global environment. Unless you *really*
know what you're doing --- and it's pretty clear that at this stage of
your development, you don't.
cheers,
Rolf Turner
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120216/0b51a78b/attachment.pl>