casperyc
2012-Mar-10 21:29 UTC
[R] function input as variable name (deparse/quote/paste) ??
Hi all
Say I have a function:
myname=function(dat,x=5,y=6){
res<<-x+y-dat
}
for various input such as
myname(dat1)
myname(dat2)
myname(dat3)
myname(dat4)
myname(dat5)
how should I modify the 'res' line, to have new informative variable
name
correspondingly, such as
dat1.res
dat2.res
dat3.res
dat4.res
dat5.res
stored in the workspace.
This is only an example of a complex function I have written.
Thanks in advance!
Casper
--
View this message in context:
http://r.789695.n4.nabble.com/function-input-as-variable-name-deparse-quote-paste-tp4462841p4462841.html
Sent from the R help mailing list archive at Nabble.com.
Hans Ekbrand
2012-Mar-10 22:46 UTC
[R] function input as variable name (deparse/quote/paste) ??
On Sat, Mar 10, 2012 at 01:29:16PM -0800, casperyc wrote:> Hi all > > Say I have a function: > > myname=function(dat,x=5,y=6){ > res<<-x+y-dat > } > > for various input such as > > myname(dat1) > myname(dat2) > myname(dat3) > myname(dat4) > myname(dat5) > > how should I modify the 'res' line, to have new informative variable name > correspondingly, such as > > dat1.res > dat2.res > dat3.res > dat4.res > dat5.res > > stored in the workspace.Why not keep the information of input values in a list, or vector? What is gained by storing that info in the variable _name_ ? Your function could return a list with both the result and the input value. While you did say that this was part of something complex, I suspect your post might be a case of "Being overly specific and not stating your real goal." -- Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net>
Thomas Lumley
2012-Mar-10 23:06 UTC
[R] function input as variable name (deparse/quote/paste) ??
On Sun, Mar 11, 2012 at 10:29 AM, casperyc <casperyc at hotmail.co.uk> wrote:> Hi all > > Say I have a function: > > myname=function(dat,x=5,y=6){ > ? ?res<<-x+y-dat > } > > for various input such as > > myname(dat1) > myname(dat2) > myname(dat3) > myname(dat4) > myname(dat5) > > how should I modify the 'res' line, to have new informative variable name > correspondingly, such as > > dat1.res > dat2.res > dat3.res > dat4.res > dat5.resYou *can* do it with myname=function(dat,x=5,y=6){ name<-paste(deparse(substitute(dat)),"res",sep=".") assign(name, x+y-dat, parent.frame(), inherits=TRUE) } but I would be very surprised if this is actually the best way to do whatever complex thing you are really doing. It's very unusual for assignments into the global workspace to be a useful R programming technique. -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland
casperyc
2012-Mar-11 17:44 UTC
[R] function input as variable name (deparse/quote/paste) ??
Thank you everyone for your reply. Like I said in my original post, this is just a demonstrative example of my 'big' self written script. My 'big' function take several inputs, of which the first 1 is the dataset and returns a LIST variable 'res<<-list()' to the workspace with many information. The names of my actual datasets are NOT in any pattern, like 'dat1', 'dat2', 'dat3'. That's why i wonder if I can modify the line 'res<<-' in anyway to be 'res.dat<<-' where 'dat' in the first input. So I can CALL via 'res.dat' (or res.newdata, res.olddata, res.tmpdata,res.hisdata) in the workspace any time I want to have a look. ----- ################################################### PhD candidate in Statistics School of Mathematics, Statistics and Actuarial Science, University of Kent ################################################### -- View this message in context: http://r.789695.n4.nabble.com/function-input-as-variable-name-deparse-quote-paste-tp4462841p4464294.html Sent from the R help mailing list archive at Nabble.com.