Re: [R] function input as variable name (deparse/quote/paste) ??

2012-03-11 Thread Hans Ekbrand
On Sat, Mar 10, 2012 at 04:01:21PM -0800, casperyc wrote:
 Sorry if I wasn't stating what I really wanted or it was a bit confusing.
 
 Basically, there are MANY datasets to run suing the same function
 
 I have written a function to analyze it and returns a LIST of useful out put
 in the variable 'res' (to the workspace).
 
 I also created another script run.r such as
 
 myname(dat1)
 myname(dat2)
 myname(dat3)
 myname(dat4)
 myname(dat5) 
 
 For now, each time the output in the main workspace 'res' (the list) is over
 written.
 
 I want it to have different suffix to differentiate them. So I can have a
 look later after the batch is run.

I see no advantage in having that information in variable names. Just

- add the name of the data set to the information that is included in
  the returned list.

- run your function with sapply() and the returned list of sapply will
  be a list of lists.

-- 
Hans Ekbrand (http://sociologi.cjb.net) h...@sociologi.cjb.net

__
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.


Re: [R] function input as variable name (deparse/quote/paste) ??

2012-03-11 Thread casperyc
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.

__
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] function input as variable name (deparse/quote/paste) ??

2012-03-10 Thread casperyc
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.

__
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.


Re: [R] function input as variable name (deparse/quote/paste) ??

2012-03-10 Thread Hans Ekbrand
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) h...@sociologi.cjb.net

__
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.


Re: [R] function input as variable name (deparse/quote/paste) ??

2012-03-10 Thread Thomas Lumley
On Sun, Mar 11, 2012 at 10:29 AM, casperyc caspe...@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.res

You *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

__
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.


Re: [R] function input as variable name (deparse/quote/paste) ??

2012-03-10 Thread casperyc
Sorry if I wasn't stating what I really wanted or it was a bit confusing.

Basically, there are MANY datasets to run suing the same function

I have written a function to analyze it and returns a LIST of useful out put
in the variable 'res' (to the workspace).

I also created another script run.r such as

myname(dat1)
myname(dat2)
myname(dat3)
myname(dat4)
myname(dat5) 

For now, each time the output in the main workspace 'res' (the list) is over
written.

I want it to have different suffix to differentiate them. So I can have a
look later after the batch is run.

Thanks.

casper

--
View this message in context: 
http://r.789695.n4.nabble.com/function-input-as-variable-name-deparse-quote-paste-tp4462841p4463044.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] function input as variable name (deparse/quote/paste) ??

2012-03-10 Thread Berend Hasselman

On 11-03-2012, at 01:01, casperyc wrote:

 Sorry if I wasn't stating what I really wanted or it was a bit confusing.
 
 Basically, there are MANY datasets to run suing the same function
 
 I have written a function to analyze it and returns a LIST of useful out put
 in the variable 'res' (to the workspace).
 

Your function uses return?
Probably not.

 I also created another script run.r such as
 
 myname(dat1)
 myname(dat2)
 myname(dat3)
 myname(dat4)
 myname(dat5) 
 
 For now, each time the output in the main workspace 'res' (the list) is over
 written.
 
 I want it to have different suffix to differentiate them. So I can have a
 look later after the batch is run.

Well, if that is the case then there is a better way than doing global 
assignments in a function.

Make sure myfunction returns the list of results with return() and don't do 
global assignment with -

for( k in 1:5) {
dataname - paste(data,k,sep=)
resname   - paste(res,k,sep=)
assign(resname, myfunction(get(dataname)))
}


Berend

__
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.