Try the following using 'lapply' and 'outer':
> myFuncs <- list(sin,sqrt) # list of functions to evaluate
> lapply(myFuncs, function(func){ # apply to each function
+ outer(1:5, 1:5, function(x,y,func){ # here are the dimensions of x &
y
+ func(x+y)
+ }, func=func)
+ })
[[1]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0.9092974 0.1411200 -0.7568025 -0.9589243 -0.2794155
[2,] 0.1411200 -0.7568025 -0.9589243 -0.2794155 0.6569866
[3,] -0.7568025 -0.9589243 -0.2794155 0.6569866 0.9893582
[4,] -0.9589243 -0.2794155 0.6569866 0.9893582 0.4121185
[5,] -0.2794155 0.6569866 0.9893582 0.4121185 -0.5440211
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1.414214 1.732051 2.000000 2.236068 2.449490
[2,] 1.732051 2.000000 2.236068 2.449490 2.645751
[3,] 2.000000 2.236068 2.449490 2.645751 2.828427
[4,] 2.236068 2.449490 2.645751 2.828427 3.000000
[5,] 2.449490 2.645751 2.828427 3.000000 3.162278
__________________________________________________________
James Holtman "What is the problem you are trying to solve?"
Executive Technical Consultant -- Office of Technology, Convergys
james.holtman at convergys.com
+1 (513) 723-2929
"Manoj - Hachibushu
Capital" To: "Liaw,
Andy" <andy_liaw at merck.com>, <r-help at stat.math.ethz.ch>
<Wanzare at hcjp.com> cc:
Sent by: Subject: RE: [R] Multiple
lapply get-around
r-help-bounces at stat.m
ath.ethz.ch
08/30/2004 09:45
> Do some calculation based on Fij
> Store Xi,Yj = Fij
To clarify the above two lines: Do some processing for each f for each j
and i and store the results in a three dimensional matrix format of i,j,f.
As you would have noticed, the outermost loop is the slowest moving one.
HTH
Manoj
-----Original Message-----
From: Liaw, Andy [mailto:andy_liaw at merck.com]
Sent: Monday, August 30, 2004 9:07 PM
To: Manoj - Hachibushu Capital; r-help at stat.math.ethz.ch
Subject: RE: [R] Multiple lapply get-around
> From: Manoj - Hachibushu Capital
>
> I am faced with a situation wherein I have to use multiple
> lapply's. The
> pseudo-code could be approximated to something as below:
>
> For each X from i=1 to n
> For each Y based on j=1 to m
> For each F from 1 to f
> Do some calculation based on Fij
> Store Xi,Yj = Fij
What does the line above mean? Are you assigning the value of Fij to
_both_
Xi and Yj? Doesn't quite make sense to me...
Andy
> End For F
> End for Y
> End for X
>
> Is there anyway to optimize the processing logic further? I *guess*
> using the multiple lapply already optimizes the logic a little bit but
> is there anything else that could be done (apart from the obvious
> solution of writing a c-code)?
>
> TIA
>
> Manoj
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html