[R] multidimensional array calculation

R. Michael Weylandt michael.weylandt at gmail.com
Fri Jan 13 20:54:21 CET 2012


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.



More information about the R-help mailing list