[R] promoting scalar arguments to vectors in a function

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Mon May 23 18:23:52 CEST 2016


Scalar values in R are just vectors of length 1. The "promotion" you are thinking of is "recycling" that automatically occurs when vectors of different lengths are supplied to certain operations. 

One shortcut might be to put them all into a data frame using the data.frame() function.  Otherwise, I think the rep() function and some conditional code would be needed. 
-- 
Sent from my phone. Please excuse my brevity.

On May 23, 2016 8:59:55 AM PDT, JLucke at ria.buffalo.edu wrote:
>R users: 
>
>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?
>
>I know in most cases this is not a problem as the promotion is
>automatic. 
>I need this for a function I am writing
>and I don't want to write any additional code to do this if I can help
>it.
>
>Joe
>Joseph F. Lucke, PhD
>Senior Statistician
>Research Institute on Addictions
>University at Buffalo
>State University of New York
>1021 Main Street
>Buffalo, NY  14203-1016
>
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list