Sean Zhang
2009-Mar-09 15:32 UTC
[R] How to write a function that accepts unlimited number of input arguments?
Dear R-helpers: I am an R newbie and have a question related to writing functions that accept unlimited number of input arguments. (I tried to peek into functions such as paste and cbind, but failed, I cannot see their codes..) Can someone kindly show me through a summation example? Say, we have input scalar, 1 2 3 4 5 then the ideal function, say sum.test, can do (1+2+3+4+5)==sum.test(1,2,3,4,5) Also sum.test can work as the number of input scalar changes. Many thanks in advance! -sean [[alternative HTML version deleted]]
baptiste auguie
2009-Mar-09 15:50 UTC
[R] How to write a function that accepts unlimited number of input arguments?
Hi, On 9 Mar 2009, at 15:32, Sean Zhang wrote:> Dear R-helpers: > I am an R newbie and have a question related to writing functions that > accept unlimited number of input arguments.it's usually through the ... argument, e.g in paste(...).> (I tried to peek into functions such as paste and cbind, but failed, I > cannot see their codes..) >simply type their name in the R prompt > paste function (..., sep = " ", collapse = NULL) .Internal(paste(list(...), sep, collapse)) <environment: namespace:base> etc... but that's not very useful here.> Can someone kindly show me through a summation example? > Say, we have input scalar, 1 2 3 4 5 > then the ideal function, say sum.test, can do > (1+2+3+4+5)==sum.test(1,2,3,4,5)see ?Reduce for one way to do this: add <- function(x) Reduce("+", x) add(list(1, 2, 3))> > Also sum.test can work as the number of input scalar changes. > > Many thanks in advance! > > -sean > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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._____________________________ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag ______________________________ [[alternative HTML version deleted]]
baptiste auguie
2009-Mar-09 16:18 UTC
[R] How to write a function that accepts unlimited number of input arguments?
On 9 Mar 2009, at 16:04, Sean Zhang wrote:> Dear Baptiste: > > Many thanks for your help! > > Using the Reduce way, it works almost perfectly. > I ran into this problem when thinking of appending vectors. > Is it possible to not use list() within add() > so add(vec1,vec2,vec3) below can work?add <- function(...) Reduce("+", list(...)) add(1, 2, 3)> Also, do you have some quick hints on using '...'? > > Many Thanks in advance. >I'm not sure of a good reference for this. I'd strongly suggest you read the Introduction to R manual ( also check the R project webpage for many other resources). Also, it'd be better if you could Cc R-help next time you ask for further information. Hope this helps, baptiste> > vec1<-c(0,1) > vec2<-c(2,3) > vec3<-c(4,5) > add <- function(x) Reduce("append", x) > add(list(vec1, vec2)) > #add(vec1,vec2) does not work at the moment > > -sean > > > > On Mon, Mar 9, 2009 at 11:50 AM, baptiste auguie > <ba208@exeter.ac.uk> wrote: > Hi, > > > On 9 Mar 2009, at 15:32, Sean Zhang wrote: > >> Dear R-helpers: >> I am an R newbie and have a question related to writing functions >> that >> accept unlimited number of input arguments. > > it's usually through the ... argument, e.g in paste(...). > >> (I tried to peek into functions such as paste and cbind, but >> failed, I >> cannot see their codes..) >> > > simply type their name in the R prompt > > > paste > function (..., sep = " ", collapse = NULL) > .Internal(paste(list(...), sep, collapse)) > <environment: namespace:base> > > etc... > > but that's not very useful here. > >> Can someone kindly show me through a summation example? >> Say, we have input scalar, 1 2 3 4 5 >> then the ideal function, say sum.test, can do >> (1+2+3+4+5)==sum.test(1,2,3,4,5) > > see ?Reduce for one way to do this: > add <- function(x) Reduce("+", x) > add(list(1, 2, 3)) >> >> Also sum.test can work as the number of input scalar changes. >> >> Many thanks in advance! >> >> -sean >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@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. > > _____________________________ > > Baptiste Auguié > > School of Physics > University of Exeter > Stocker Road, > Exeter, Devon, > EX4 4QL, UK > > Phone: +44 1392 264187 > > http://newton.ex.ac.uk/research/emag > ______________________________ > >_____________________________ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag ______________________________ [[alternative HTML version deleted]]
Gabor Grothendieck
2009-Mar-09 21:59 UTC
[R] How to write a function that accepts unlimited number of input arguments?
Try this: sum.test <- function(...) sum(c(...)) More commonly one uses the list(...) construct. On Mon, Mar 9, 2009 at 11:32 AM, Sean Zhang <seanecon at gmail.com> wrote:> Dear R-helpers: > I am an R newbie and have a question related to writing functions that > accept unlimited number of input arguments. > (I tried to peek into functions such as paste and cbind, but failed, I > cannot see their codes..) > > Can someone kindly show me through a summation example? > Say, we have input scalar, ?1 2 3 4 5 > then the ideal function, say sum.test, can do > (1+2+3+4+5)==sum.test(1,2,3,4,5) > > Also sum.test can work as the number of input scalar changes. > > Many thanks in advance! > > -sean > > ? ? ? ?[[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. >