> 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).
Not quite enough
gt;> although this does work:
>>
>> do.call(func2, as.list(c(10, 121)))
>>
>> And, this also works:
>>
>> apply(expand.grid(temps,times), 1, function(a) do.call("+", as.list(a)))
>>
>> There is some subtlety here I don't understand.
tion(a) do.call("+", as.list(a)))
>>
>> There is some subtlety here I don't understand.
>>
>> Thanks,
>>
>> Steve
>>
>> -Original Message-
>> From: Jeff Newmiller [mailto:jdnew...@dcn.davis.ca.us]
>> Sent: Friday
is.ca.us]
Sent: Friday, September 09, 2016 5:39 PM
To: Steve Kennedy; r-help@r-project.org
Subject: Re: [R] Apply a multi-variable function to a vector
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
instance
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
Try do.call(), as in
> func2 <- function(time, temp) paste(time, temp)
> func2(121, 10)
[1] "121 10"
> do.call(func2, as.list(c(121,10)))
[1] "121 10"
> do.call(func2, list(121,10))
[1] "121 10"
>
> func2(121, time=10:12)
[1] "10 121" "11 121" "12 121"
> do.call(func2, list(121,time=10:12))
[1]
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
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
8 matches
Mail list logo