Hello again!
thanks to all who helped with overlay plots - v. easy in the end.
Anyway, another new(ba)bee type question - the gurus will cringe I'm sure!
Q. simple R function
mm <- function (u) {
x <- u$GDP
x
m <- mean(x)
m
}
When the function is called the vector "x" does not get printed from
within
the function, but the mean value "m" does, why?
I think it's probably to do with scoping but don't really know.
The vector "x" can of course be printed with the print function so
there no
problem seeing it for program debugging etc.
Thanks in advance.
Gerard Keogh
The information in this email, and any attachments transmitted with it, are
confidential
and are for the intended recipient only. If you receive this message in error,
please
notify us via postmaster at cso.ie.
To see the latest figures from the CSO go to http://www.cso.ie
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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 again! > thanks to all who helped with overlay plots - v. easy in the end. > > Anyway, another new(ba)bee type question - the gurus will cringe I'm sure! > > Q. simple R function > mm <- function (u) { > x <- u$GDP > x > m <- mean(x) > m > }m is returned by the function mm while x is not. If you would like to return both x and m use mm <- function (u) { x <- u$GDP x m <- mean(x) list(m = m, x = x) } which returns a names list with elements m and x Torsten> > When the function is called the vector "x" does not get printed from within > the function, but the mean value "m" does, why? > I think it's probably to do with scoping but don't really know. > The vector "x" can of course be printed with the print function so there no > problem seeing it for program debugging etc. > > Thanks in advance. > > Gerard Keogh > > The information in this email, and any attachments transmitted with it, are confidential > and are for the intended recipient only. If you receive this message in error, please > notify us via postmaster at cso.ie. > > To see the latest figures from the CSO go to http://www.cso.ie > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
A function returns only a single result in R. A way to get several results from a function is by using a list:> mm <- function(u) {+ x <- u$GDP + m <- mean(x) + res <- list(data=x, mean=m) + res + }> a <- NULL > a$GDP <- 1:10 > a.mm <- mm(a) > a.mm$data[1] 1 2 3 4 5 6 7 8 9 10> a.mm$mean[1] 5.5 All the best, Philippe Grosjean ...........]<(({?<...............<?}))><............................... ) ) ) ) ) __ __ ( ( ( ( ( |__) | _ ) ) ) ) ) | hilippe |__)rosjean ( ( ( ( ( Marine Biol. Lab., ULB, Belgium ) ) ) ) ) __ ( ( ( ( ( |\ /| |__) ) ) ) ) ) | \/ |ariculture & |__)iostatistics ( ( ( ( ( ) ) ) ) ) e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org ( ( ( ( ( SciViews project coordinator (http://www.sciviews.org) ) ) ) ) ) tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home) ( ( ( ( ( ) ) ) ) ) "I'm 100% confident that p is between 0 and 1" ( ( ( ( ( L. Gonick & W. Smith (1993) ) ) ) ) ) ....................................................................... -----Message d'origine----- De : owner-r-help at stat.math.ethz.ch [mailto:owner-r-help at stat.math.ethz.ch]De la part de Gerard.Keogh at cso.ie Envoye : lundi 10 septembre 2001 17:18 A : r-help at lists.r-project.org Objet : [R] ?? hmm ?? Hello again! thanks to all who helped with overlay plots - v. easy in the end. Anyway, another new(ba)bee type question - the gurus will cringe I'm sure! Q. simple R function mm <- function (u) { x <- u$GDP x m <- mean(x) m } When the function is called the vector "x" does not get printed from within the function, but the mean value "m" does, why? I think it's probably to do with scoping but don't really know. The vector "x" can of course be printed with the print function so there no problem seeing it for program debugging etc. Thanks in advance. Gerard Keogh The information in this email, and any attachments transmitted with it, are confidential and are for the intended recipient only. If you receive this message in error, please notify us via postmaster at cso.ie. To see the latest figures from the CSO go to http://www.cso.ie -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
To add to the other answers,
1) Normally, the last statement in a function determines its return value.
2) Normally, when a function returns to the top level (the ">"
prompt),
its value is printed by calling print().
Thus your function returns the value of "m", which gets printed when
it
returns to the top level. To see the value of "x", you can either
return it
and the value of "m" by changing the last line to
"return(x,m)", or you can
call "print(x)" inside the function.
-Greg
-----Original Message-----
From: Gerard.Keogh at cso.ie [mailto:Gerard.Keogh at cso.ie]
Sent: Monday, September 10, 2001 11:18 AM
To: r-help at hypatia.math.ethz.ch
Subject: [R] ?? hmm ??
Hello again!
thanks to all who helped with overlay plots - v. easy in the end.
Anyway, another new(ba)bee type question - the gurus will cringe I'm sure!
Q. simple R function
mm <- function (u) {
x <- u$GDP
x
m <- mean(x)
m
}
When the function is called the vector "x" does not get printed from
within
the function, but the mean value "m" does, why?
I think it's probably to do with scoping but don't really know.
The vector "x" can of course be printed with the print function so
there no
problem seeing it for program debugging etc.
Thanks in advance.
Gerard Keogh
The information in this email, and any attachments transmitted with it, are
confidential
and are for the intended recipient only. If you receive this message in
error, please
notify us via postmaster at cso.ie.
To see the latest figures from the CSO go to http://www.cso.ie
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be
privileged. It is intended for the addressee(s) only. Access to this E-mail by
anyone else is unauthorized. If you are not an addressee, any disclosure or
copying of the contents of this E-mail or any action taken (or not taken) in
reliance on it is unauthorized and may be unlawful. If you are not an addressee,
please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
At 16:17 10/09/01 +0100, you wrote:>Hello again! >thanks to all who helped with overlay plots - v. easy in the end. > >Anyway, another new(ba)bee type question - the gurus will cringe I'm sure! > >Q. simple R function >mm <- function (u) { > x <- u$GDP > x > m <- mean(x) > m >} > >When the function is called the vector "x" does not get printed from within >the function, but the mean value "m" does, why? >I think it's probably to do with scoping but don't really know. >The vector "x" can of course be printed with the print function so there no >problem seeing it for program debugging etc. > >Thanks in advance. > >Gerard KeoghIf you want to have the results just printed, you can modify your function by adding print(x): mm <- function (u) { x <- u$GDP print(x) m <- mean(x) m } Emmanuel Paradis Laboratoire de Pal?ontologie Institut des Sciences de l'?volution Universit? Montpellier II F-34095 Montpellier c?dex 05 France phone: +33 4 67 14 39 64 fax: +33 4 67 14 36 10 mailto:paradis at isem.univ-montp2.fr -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 10 Sep 2001 Gerard.Keogh at cso.ie wrote:> Hello again! > thanks to all who helped with overlay plots - v. easy in the end. > > Anyway, another new(ba)bee type question - the gurus will cringe I'm sure! > > Q. simple R function > mm <- function (u) { > x <- u$GDP > x > m <- mean(x) > m > } > > When the function is called the vector "x" does not get printed from within > the function, but the mean value "m" does, why?Actually neither x nor m gets printed from within the function, because you didn't ask R to print either of them. The statement x means "Work out what x is". To print x you need print(x) which means "Work out what x is and print the result" The thing that is printed when you type mm(u) at the command line is not m but mm(u). At the command line, only, a calculated value that isn't assigned to any other variable will be printed. So mm(u) at the command line means "Work out the value of mm(u)", and since it is at the command line and the result is not assigned to anything it is printed. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._