[R] Elementary sapply question

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jun 21 21:13:48 CEST 2004


Named arguments are matched by name.  Unnamed arguments are matched by 
position.

S's argument matching rules are quite complex.  It normally helps clarity
to name arguments in all but the simplest calls.

rep(1, 4) and rep(times=1, x=4) are different calls -- the second could 
be called perverse, but at least you don't need to remember the arg seqne 
of rep().  (How many of you know it by heart?  Is each before or 
after length.out?)

On Mon, 21 Jun 2004, Brian Desany wrote:

>  
> Looking in ?mapply, I executed the examples:
> 
> > mapply(rep, 1:4, 4:1)
> [[1]]
> [1] 1 1 1 1
> 
> [[2]]
> [1] 2 2 2
> 
> [[3]]
> [1] 3 3
> 
> [[4]]
> [1] 4
> 
> > mapply(rep, times=1:4, x=4:1)
> [[1]]
> [1] 4
> 
> [[2]]
> [1] 3 3
> 
> [[3]]
> [1] 2 2 2
> 
> [[4]]
> [1] 1 1 1 1
> 
> 
> I can guess that because these are 2 examples, it is no surprise that the
> results are different. Why is this? If ?mapply is giving me a clue, I'm not
> seeing it.
> 
> Thanks,
> -Brian.
> 
> 
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Liaw, Andy
> Sent: Monday, June 21, 2004 11:50 AM
> To: 'Ajay Shah'; r-help
> Subject: RE: [R] Elementary sapply question
> 
> At least two ways:
> 
> 1. Use extra argument in the function being sapply()'ed; e.g.,
> 
> > f <- function(x, y) x*x + y*y
> > x <- 3:5
> > sapply(x, f, 3)
> [1] 18 25 34
> 
> [See the "..." argument in ?sapply.]
> 
> 2. More generally, if both x and y are vectors (of the same length), then
> you can use mapply(); e.g.,
> 
> > x <- 1:3
> > y <- 3:5
> > pyth <- function(x, y) x*x + y*y
> > mapply(pyth, x, y)
> [1] 10 20 34
> 
> HTH,
> Andy
> 
> > From: Ajay Shah
> > 
> > I am discovering sapply! :-) Could you please help me with a very
> > elementary question?
> > 
> > Here is what I know. The following two programs generate the 
> > same answer.
> > 
> > --------------------------------+-----------------------------
> > -----------
> >        Loops version            |          sapply version
> > --------------------------------+-----------------------------
> > -----------
> >                                 |
> > f <- function(x) {              |       f <- function(x) { 
> >   return(x*x)                   |         return(x*x)      
> > }                               |       }                  
> > values = c(2,4,8)               |       values = c(2,4,8)  
> > answers=numeric(3)              |       answers = sapply(values, f)
> > for (i in 1:3) {                |       
> >   answers[i] = f(values[i])     |
> > }                               |
> > 
> > and this is cool!
> > 
> > My problem is this. Suppose I have:
> >      pythagorean <- function(x, y) {
> >        return(x*x + y*y)
> >      }
> > 
> > then how do I utilise sapply to replace
> >      fixed.x = 3
> >      y.values = c(3,4,5)
> >      answers=numeric(3)
> >      for (i in 1:3) {
> >          answers[i] = pythagorean(fixed.x, y.values[i])
> >      }
> > 
> > ?
> > 
> > I have read the sapply docs, and don't know how to tell him that the
> > list values that he'll iterate over "fit in" as y.values[i].
> > 
> > -- 
> > Ajay Shah                                                   Consultant
> > ajayshah at mayin.org                      Department of Economic Affairs
> > http://www.mayin.org/ajayshah           Ministry of Finance, New Delhi
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! 
> > http://www.R-project.org/posting-guide.html
> > 
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 

-- 
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




More information about the R-help mailing list