Charles RAUX wrote:> > After searching in R- Introduction, FAQ, help... I don't understand > this: > I write a function in a file (.R): > tt <- function(mc) { date() > mc<-read.csv2("machines.txt",na.strings="") > date() > } > > I source it in R and I type tt(). The answer is > > tt() > [1] "Mon Aug 14 11:18:25 2000" > > > The instructions following the first "date()" are ignored. Why?They are NOT ignored. Please read the FAQs (and the help pages). If you want to return mc, you should specify it. In your case the last expression (date()) will be returned. Why do you use the argument "mc" in your function, if you don't use it? The function tt <- function(filename) { cat(date(), "\n") mc <- read.csv2(filename, na.strings="") return(mc) } might do what you want. Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

After searching in R- Introduction, FAQ, help... I don't understand this: I write a function in a file (.R): tt <- function(mc) { date() mc<-read.csv2("machines.txt",na.strings="") date() } I source it in R and I type tt(). The answer is> tt()[1] "Mon Aug 14 11:18:25 2000">The instructions following the first "date()" are ignored. Why? --------------- Charles RAUX, Laboratoire d'Economie des Transports CNRS-Universit? Lumi?re Lyon 2-ENTPE email : charles.raux at let.ish-lyon.cnrs.fr http://www.ish-lyon.cnrs.fr/let -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Since I have large data bases I only try to write a little function that gives the current time, reads a data frame (and assigns it) and at end gives the time. I have tried many forms but no one works. Le 14 Aug 00 ? 21:53, Robert Gentleman ?crivait:> Date: Mon, 14 Aug 2000 21:53:37 +1200 > From: Robert Gentleman <rgentlem at stat.auckland.ac.nz> > To: Charles RAUX <Charles.Raux at let.ish-lyon.cnrs.fr> > Subject: Re: [R] Writing a workable function> On Mon, Aug 14, 2000 at 11:25:35AM +0100, Charles RAUX wrote: > > After searching in R- Introduction, FAQ, help... I don't understand > > this: > > I write a function in a file (.R): > > tt <- function(mc) { date() > > mc<-read.csv2("machines.txt",na.strings="") > > date() > > } > > > > I source it in R and I type tt(). The answer is > > > tt() > > [1] "Mon Aug 14 11:18:25 2000" > > > > > The instructions following the first "date()" are ignored. Why? > > Actually they aren't. But perhaps your problem is that you don't > realize that in R (as in many other languages, but certainly not all) > parameters are passed by value. Thus, this is no way to assign a value > to mc. > > tt<-function() read.csv2("machines.txt",na.strings="") > mc<-tt() > > does what you seem to want. > > Notice that the value printed out, > [1] "Mon Aug 14 11:18:25 2000" > is, in some sense, not the result of a call to date, both of those > are invisible at top level. Rather, the last call to date returns > that string as its value and it is that value that is returned by > the function and subsequently printed. > > --------------- > > Charles RAUX, > > Laboratoire d'Economie des Transports > > CNRS-Universit? Lumi?re Lyon 2-ENTPE > > email : charles.raux at let.ish-lyon.cnrs.fr > > http://www.ish-lyon.cnrs.fr/let > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > > Send "info", "help", or "[un]subscribe" > > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ > > -- > +-------------------------------------------------------------------------+ > | Robert Gentleman phone : (64-9) 3737-599 ext 3946 | > | Senior Lecturer fax : (64-9) 3737-018 | > | Department of Statistics office : Room 206 Maths/Physics | > | University of Auckland email : rgentlem at stat.auckland.ac.nz | > +-------------------------------------------------------------------------+ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Hello! On 14-Aug-00 Charles RAUX wrote:> After searching in R- Introduction, FAQ, help... I don't understand > this: > I write a function in a file (.R): > tt <- function(mc) { date() > mc<-read.csv2("machines.txt",na.strings="") > date() > } > > I source it in R and I type tt(). The answer is >> tt() > [1] "Mon Aug 14 11:18:25 2000" >> > The instructions following the first "date()" are ignored. Why?I don't think they are ignored, but you don't print out, give back, resp., any value. So if you want as output a list of date and the data.frame do: tt <- function{ mc <- read.csv2("machines.txt",na.strings="") mc <- list(date(),mc) mc #This prints out or gives back the value of mc in this case the date #and the data.frame! } In my (old!) version of R-Notes there is an example of a function on page 30, but it lacks the explanation for the last line... I don't have the time to check the FAQ right now, but I'm sure it's in there somewhere! Cheers, Winfried ---------------------------------- E-Mail: Winfried Theis <theis at statistik.uni-dortmund.de> Date: 14-Aug-00 Tel.: +49-231-755-5903 Room: Mathematikgebäude, M832 Dipl.-Math. Winfried Theis, Graduiertenkolleg "Angewandte Statistik", Universität Dortmund, 44221 Dortmund ---------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Charles Raux asks:> After searching in R- Introduction, FAQ, help... I don't understand > this: > I write a function in a file (.R): > > tt <- function(mc) { > date() > mc <- read.csv2("machines.txt", na.strings = "") > date() > } > > I source it in R and I type tt(). The answer is > > tt() > [1] "Mon Aug 14 11:18:25 2000" > > > The instructions following the first "date()" are ignored. Why?This is a very common misunderstanding by people who come from languages where macros are the norm rather than genuine functions. When a function in R is executed all variables created or modified within the function are local to the function and only the value of the function is communicated to the wider calculation. In your case the value of the function is the second call to date() so all other operations within the function are done, but ultimately lost. Note that the arguments are also not linked with the actual arguments, so your use of mc as an argument is irrelevant. What you seem to want to do is write a function to read in data and perhaps to some extra bits and pieces to it afterwards that you have not shown us yet. The standard way to go about this would be to write the function as follows: tt <- function() { mc <- read.csv2("machines.txt", na.strings = "") .... (further operations on mc as needed) .... mc # makes the final value of mc the value of the function } and you would call the function from the command line using this.mc <- tt() that is, you would assign the value of the function to some top-level variable that you would then use in further calculations. It is possible to write R functions so that they do behave like macros in the style you might expect, but that is really taking a giant leap backwards and I simply won't tell you how to do it... :-) Bill Venables. -- Bill Venables, Statistician, CMIS Environmetrics Project CSIRO Marine Labs, PO Box 120, Cleveland, Qld, AUSTRALIA. 4163 Tel: +61 7 3826 7251 Email: Bill.Venables at cmis.csiro.au Fax: +61 7 3826 7304 http://www.cmis.csiro.au/bill.venables/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Hi all, I want to replace a couple of (Excel) steps in processing the output of some biological simulations - it should be easy for you guys so I was hoping for a quickstart from someone . . I want to read values from a text file - 200 lines, 32 floats per line - and calculate a mean for each of the 32 values, so I would end up with an "x" vector of 1-200 and a "y" vector of the 200 means. Thanks, Phil. - Philip Rhoades Pricom Pty Limited (ACN 003 252 275) GPO Box 3411 Sydney NSW 2001 Australia Mobile: +61:0411-185-652 Fax: +61:2:8923-5363 E-mail: philr at mail.austasia.net -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._