Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible Url : https://stat.ethz.ch/pipermail/r-help/attachments/20080312/5dee1048/attachment.pl
You just need to return the values computed when a==0 and when a!=0.
a=0
f=function(x,y,z=0) {
if (a==0) return(x[1]+x[2]+y)
if (a!=0) return(x[1]+x[2]+y+z)
}
> f(1:2,3)
[1] 6>
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Dwayne Blind
Sent: Wednesday, March 12, 2008 1:59 PM
To: r-help at r-project.org
Subject: [R] default values
Dear R users,
I wrote the following toy example to explain my problem :
a=0
f=function(x,y,z) {
if (a==0) x[1]+x[2]+y
if (a!=0) x[1]+x[2]+y+z
}
f(1:2,3)
I have not specified z and I get an error. Although a=0, R seems to want to
know z because it's in the expression x[1]+x[2]+y+z.
So I tried to put a default value :
a=0
f=function(x,y,z=0) {
if (a==0) x[1]+x[2]+y
if (a!=0) x[1]+x[2]+y+z
}
f(1:2,3)
Why isn't it working ? Sometimes everything is fine even though a parameter
is not specified.
Thanks a lot.
[[alternative HTML version deleted]]
______________________________________________
R-help at 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.
On Wed, 12 Mar 2008, Dwayne Blind wrote:> Dear R users, > > I wrote the following toy example to explain my problem : > > a=0 > f=function(x,y,z) { > if (a==0) x[1]+x[2]+y > if (a!=0) x[1]+x[2]+y+z > } > f(1:2,3) > > > I have not specified z and I get an error.What was the error? It works for me (so I've no idea), and returns NULL, the value of the last expression (invisibly). I think you intended f <- function(x, y, z) if (a==0) x[1]+x[2]+y else x[1]+x[2]+y+z or f <- function(x, y, z) ifelse(a==0, x[1]+x[2]+y, x[1]+x[2]+y+z)> Although a=0, R seems to want to > know z because it's in the expression x[1]+x[2]+y+z. > > So I tried to put a default value : > > a=0 > f=function(x,y,z=0) { > if (a==0) x[1]+x[2]+y > if (a!=0) x[1]+x[2]+y+z > } > f(1:2,3) > > Why isn't it working ? Sometimes everything is fine even though a parameter > is not specified. > > Thanks a lot. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595