[R] promoting scalar arguments to vectors in a function

S Ellison S.Ellison at LGCGroup.com
Mon May 23 18:11:35 CEST 2016


> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of
> Suppose I have a function that takes three numeric arguments x, y,  and z, any
> of which may be scalars or vectors.
> Suppose further that the user takes one of the arguments, say y, as a vector
> with the other two as scalars.
> Is there an existing R function that will promote the other two arguments to
> vectors of the same size?

This depends on what you mean by 'promote' ...

If you want to do something like 'recycling', as is often done for plot colours, but need things the same length for subsetting etc, one crude but fairly straightforward way I will confess to using in a function is something like

L <- max( length(x), length(y), length(z) )
x <- rep(x, length.out=L) 
y <- rep(y, length.out=L) 
z <- rep(z, length.out=L)

If you wanted to zero-fill to the same length, or fill with NA, that'd be something else ...

S Ellison




*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list