Skip to content

Apply a multi-variable function to a vector

3 messages · Steve Kennedy, Jeff Newmiller, S Ellison

#
Hello,

I would like to define an arbitrary function of an arbitrary number of variables, for example, for 2 variables:

func2 <- function(time, temp) time + temp

I'd like to keep variable names that have a meaning in the problem (time and temperature above).

If I have a vector of values for these variables, for example in the 2-d case, c(10, 121), I'd like to apply my function (in this case func2) and obtain the result. Conceptually, something like,

func2(c(10,121))

becomes

func2(10,121)

Is there a simple way to accomplish this, for an arbitrary number of variables?  I'd like something that would simply work from the definition of the function.  If that is possible.

Thanks,

Steve Kennedy

CONFIDENTIALITY NOTICE: This e-mail message, including a...{{dropped:11}}
#
Your architecture has a bad smell to me. For one thing you are mixing different units in the same vector but should be putting multiple instances of the same variable into one vector. Lists of vectors (data frames) are typically used when multiple variables need to be grouped.

Another problem is that you are constraining the names of the variables you pass to the function to be named the same as they are inside the function. This really limits your use of those functions.

There really is too much abstraction going on here.
3 days later
#
Not quite enough information here.

If we called 
func2(30, 298, 23)

which has an arbitrary third argument, what would you like to happen to the third argument? And a fourth, fifth and so on? Something consistent for all arguments, or something different for each depending on its (arbitrary) name?


S Ellison





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