Does this version of y do what you want?

y=function(j)sum(sapply(1:3,function(i)x(i,j)))


                                        - Phil Spector
                                         Statistical Computing Facility
                                         Department of Statistics
                                         UC Berkeley
                                         spec...@stat.berkeley.edu


On Sat, 13 Nov 2010, Hiba Baroud wrote:


Hi

I'm trying to sum functions of lists with different lengths. Here is a 
simplified example of the problem:

r=list(1:3,1:5,1:2)

r
[[1]]
[1] 1 2 3
[[2]]
[1] 1 2 3 4 5
[[3]]
[1] 1 2

x=function(i,j) sum(j*r[[i]])                                # x is a function of 
two parameters: i & j

y=function(j)                                                     # y is the 
sum of x over i
+ {
+ s=seq(from=1,to=3,by=1)
+ sum(x(s,j))
+ }

y(1)
Error in r[[i]] : recursive indexing failed at level 2

The error is clearly due to the lists; I actually tried this code with 
functions of vectors and scalars and it worked perfectly.
I tried to use means of summing lists by recursion but it does not work with 
functions of lists, for e.g.:

y=function(j) x(1,j)
for(i in 2:3)
+ {
+ y=function(j) y(j)+x(i,j)
+ }

y(1)
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

Is there a way to perform this summation?

Thanks!

H.

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.


______________________________________________
R-help@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.

Reply via email to