[R] How can I assign an argument to transfer whether by ref or by value?
Gabor Grothendieck
ggrothendieck at gmail.com
Sun Nov 6 19:49:08 CET 2005
i think this question was already answered but just to elaborate,
pass by value means that a copy of the argument is passed to the
function so if the argument is changed in the function its not changed
in the caller. Pass by reference means its changed in the caller too.
R passes by value although there are workarounds to pass by
reference. In the following f uses pass by value and g pass by
reference.
> y <- 1
> f <- function(x) x<-x+1
> f(y)
> y
[1] 1
> g <- function(x) eval.parent(substitute(x <- x+1))
> g(y)
> y
[1] 2
On 11/6/05, Adaikalavan Ramasamy <ramasamy at cancer.org.uk> wrote:
> I do not understand what your question is. Can you clarify with an
> example or analogies to other programming language.
>
> my.fun <- function(x, y=1){ x^y }
>
> my.fun(5) # returns 5
> my.fun(5, 2) # returns 25
> my.fun(y=2, x=5) # returns 25
>
> Regards, Adai
>
>
> On Sun, 2005-11-06 at 03:28 +0000, Xiaofan Li wrote:
> > Hello guys,
> >
> > I am wondering the default way of transferring arguments in R. Is it by
> > value or by ref in default case, or could that be changed explicitly?
> >
> > Cheers,
> > Xiaofan
> >
> > ---
> > Xiaofan Li
> > Department of Applied Mathematics and Theoretical Physics
> > University of Cambridge
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
> --
> Adaikalavan Ramasamy ramasamy at cancer.org.uk
> Centre for Statistics in Medicine http://www.ihs.ox.ac.uk/csm/
> Wolfson College Annexe Tel : 01865 284 408
> Linton Road, Oxford OX2 6UD Fax : 01865 284 424
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list