[R] Stuck in trying to convert repetitive code into a function

Simon Fear Simon.Fear at synequanon.com
Wed Mar 3 10:22:52 CET 2004


I can't debug this because you haven't said what
levels2weeklyret() does. However, I can offer a tip:
use traceback() straight after the error to see exactly
where it goes wrong. If still baffled, use debug() [takes
a bit of learning].

To return multiple values, return a list (see ?return) or
other multi-component structure.

If I may offer another word of advice: don't use single
letter variable names, especially l which is identical to 1
in many fonts, and don't use f or fn except for 
a function name. Also we are all supposed to use names
like `eatFile` rather than the old-style `eat.file`, though R 
base itself is full of names.somethings and probably 
always will be.

HTH

> -----Original Message-----
> From: Ajay Shah [mailto:ajayshah at mayin.org]
> Sent: 02 March 2004 18:00
> To: r-help
> Subject: [R] Stuck in trying to convert repetitive code into 
> a function
> 
> 
> Security Warning: 
> If you are not sure an attachment is safe to open contact 
> Andy on x234. 
> There are 0 attachments with this message. 
> ________________________________________________________________ 
>  
> Folks,
> 
> I have the following repetitive code which works correctly:
> 
>    A = read.table(file="junior.csv", sep=",", 
> col.names=c("date", "l"));
>    A$date = chron(as.character(A$date), format="m/d/y");
>    r.junior = levels2weeklyret(lastfriday, A$date, A$l);
>    plot(A$date, A$l, type="l", col="red", main="Junior levels")
>    z <- locator(1)
> 
>    A = read.table(file="kospi.csv", sep=",", 
> col.names=c("date", "l"));
>    A$date = chron(as.character(A$date), format="m/d/y");
>    r.kospi = levels2weeklyret(lastfriday, A$date, A$l);
>    plot(A$date, A$l, type="l", col="red", main="Kospi levels")
>    z <- locator(1)
> 
> I tried to write it more nicely as follows:
> 
>    eat.a.file <- function(fn, label, lastfriday) {
>      f = read.table(file=fn, sep=",", col.names=c("date", "l"));
>      f$date = chron(as.character(f$date), format="m/d/y");
>      plot(f$date, f$l, type="l", col="red", main=label);
>      z <- locator(1);
>      return(levels2weeklyret(lastfriday, f$date, f$l));
>    }
>    r.junior = eat.a.file(fn="junior.csv", label="Junior", lastfriday);
>    r.kospi  = eat.a.file(fn="kospi.csv", label="Kospi", lastfriday);
> 
> When I do this, I get this error at the first of the two 
> function calls:
> 
> Error in vector("double", length) : negative length vectors 
> are not allowed
> Execution halted
> 
> I have tried hard to experiment with various ways of writing it, but
> am truly stumped. Any ideas on what might be going wrong? I am new to
> R and have only written a few functions so far.
> 
> On a slightly related note, what is the situation in R on passing by
> reference versus passing by value? I looked at the standard docs and
> they seem to insist that a function can only send back one return
> value. How does a function send back multiple things that it has
> created?
> 
> -- 
> Ajay Shah                                                   Consultant
> ajayshah at mayin.org                      Department of Economic Affairs
> http://www.mayin.org/ajayshah           Ministry of Finance, New Delhi
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html  
 
Simon Fear 
Senior Statistician 
Syne qua non Ltd 
Tel: +44 (0) 1379 644449 
Fax: +44 (0) 1379 644445 
email: Simon.Fear at synequanon.com 
web: http://www.synequanon.com 
  
Number of attachments included with this message: 0 
  
This message (and any associated files) is confidential and\...{{dropped}}




More information about the R-help mailing list