Hello,
probably it is quite easy but I can get it: I have
mulitple numeric vectors and a function using
all of them to calculate a new value:
L <- c(200,400,600)
AR <- c(1.5)
SO <- c(1,3,5)
T <- c(30,365)
fun <- function(L,AR,SO,T){
exp(L*AR+sqrt(SO)*log(T))
}
How can I get an array or dataframe where
all possible combinations of the factors are listed
and the new value is calculated.
I thought about an array like:
array(NA, dim = c(3,1,3,2),
dimnames=list(c(200,400,600),c(1.5),c(1,3,5),c(30,365)))
but how can I get the array populated according to the function?
As I want to get in the end a 2D dataframe I probably will use the melt.array()
function from the reshape package or is there another way to get simple such
a "full-factorial" dataframe with all possible combinations?
Best regards,
Johannes
See
?expand.grid
For example,
df <- expand.grid(L=L, AR=AR, SO=SO, T=T)
df$y <- fun(df$L, df$AR, df$SO, df$T)
Jean
Johannes Radinger wrote on 01/13/2012 12:28:46 PM:
> Hello,
>
> probably it is quite easy but I can get it: I have
> mulitple numeric vectors and a function using
> all of them to calculate a new value:
>
> L <- c(200,400,600)
> AR <- c(1.5)
> SO <- c(1,3,5)
> T <- c(30,365)
>
> fun <- function(L,AR,SO,T){
> exp(L*AR+sqrt(SO)*log(T))
> }
>
> How can I get an array or dataframe where
> all possible combinations of the factors are listed
> and the new value is calculated.
>
> I thought about an array like:
> array(NA, dim = c(3,1,3,2), dimnames=list(c(200,400,600),c(1.5),c(1,
> 3,5),c(30,365)))
>
> but how can I get the array populated according to the function?
>
> As I want to get in the end a 2D dataframe I probably will use the
> melt.array()
> function from the reshape package or is there another way to get simple
such> a "full-factorial" dataframe with all possible combinations?
>
> Best regards,
> Johannes
[[alternative HTML version deleted]]
Perhaps repeated use of the outer() function. You could also write a multi.outer() or adopt one of the solutions here: http://stackoverflow.com/questions/6192848/how-to-generalize-outer-to-n-dimensions Michael On Fri, Jan 13, 2012 at 1:28 PM, Johannes Radinger <jradinger at gmx.at> wrote:> Hello, > > probably it is quite easy but I can get it: I have > mulitple numeric vectors and a function using > all of them to calculate a new value: > > L <- c(200,400,600) > AR <- c(1.5) > SO <- c(1,3,5) > T <- c(30,365) > > fun <- function(L,AR,SO,T){ > ? ? ? ?exp(L*AR+sqrt(SO)*log(T)) > } > > How can I get an array or dataframe where > all possible combinations of the factors are listed > and the new value is calculated. > > I thought about an array like: > array(NA, dim = c(3,1,3,2), dimnames=list(c(200,400,600),c(1.5),c(1,3,5),c(30,365))) > > but how can I get the array populated according to the function? > > As I want to get in the end a 2D dataframe I probably will use the melt.array() > function from the reshape package or is there another way to get simple such > a "full-factorial" dataframe with all possible combinations? > > Best regards, > Johannes > ______________________________________________ > R-help at r-project.org mailing list > 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.