[R] sum of list elements

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Thu Mar 4 16:13:01 CET 2010


do these lists contain 3D arrays of the same dimensions? If yes, then 
you could use

Reduce("+",  pred.svm[[i]])[1,2,5]

otherwise a for-loop will also be clear and efficient, e.g.,

W <- pred.svm[[i]][[1]][1,2,5]
for (j in 2:20) {
     W <- W + pred.svm[[i]][[j]][1,2,5]
}


I hope it helps.

Best,
Dimitris


On 3/4/2010 4:02 PM, Eleni Christodoulou wrote:
> Dear list,
>
> I have some difficulty in manipulating list elements. More specifically, I
> am performing svm regression and have a list of lists, called pred.svm. The
> elements of the second list are 3D arrays. Thus I have pred.svm[[i]][[j]],
> with 1<=i<=5 and 1<=j<=20.
> I want to take the sum of the elements a specific array dimension across all
> j, for one i. Mathematically speaking, I want to calculate *W* as:
>
>            *W = pred.svm[[i]][[1]][1,2,5] + pred.svm[[i]][[2]][1,2,5]+
> pred.svm[[i]][[3]][1,2,5]+...+ pred.svm[[i]][[20]][1,2,5]*
>
> I have tried to apply the *lapply() *function but it seems that its
> arguments can only be vector elements of a list...Do I need to convert the
> array data to vector data?
>
> Any advice would be very welcome!
>
> Thanks a lot,
> Eleni
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-help mailing list