[R] sum of list elements
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@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.
Re: [R] sum of list elements
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@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 __ 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.
Re: [R] sum of list elements
Thank you Dimitris! I have 3D arrays of the same dimensions, so Reduce worked... Best, Eleni On Thu, Mar 4, 2010 at 5:13 PM, Dimitris Rizopoulos d.rizopou...@erasmusmc.nl wrote: 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@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 [[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.
Re: [R] sum of list elements
My guess is that the original poster is expecting a matrix not a scalar. If so, then: Reduce('+', a) [,1] [,2] [,3] [1,]300 [2,]030 [3,]003 Patrick Burns patr...@burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of The R Inferno and A Guide for the Unwilling S User) David Winsemius wrote: On Jul 2, 2009, at 11:24 PM, David Winsemius wrote: On Jul 2, 2009, at 10:20 PM, Ivo Shterev wrote: a = list(diag(3), diag(3), diag(3)) a = list(diag(3), diag(3), diag(3)) sum(unlist(a)) [1] 9 Also: sum(sapply(a,I)) [1] 9 David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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.
[R] sum of list elements
Dear All, Is there an automatic way to sum the elements of a list? For example given a = list(diag(3), diag(3), diag(3)) to obtain b=a[1]+a[2]+a[3]. Thanks Ivo __ 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.
Re: [R] sum of list elements
I think you are asking for Reduce Reduce(`+`, a) [,1] [,2] [,3] [1,]300 [2,]030 [3,]003 You might be asking for do.call with some function's name as its first argument. do.call(sum, a) [1] 9 __ 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.
Re: [R] sum of list elements
On Jul 2, 2009, at 10:20 PM, Ivo Shterev wrote: a = list(diag(3), diag(3), diag(3)) a = list(diag(3), diag(3), diag(3)) sum(unlist(a)) [1] 9 David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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.
Re: [R] sum of list elements
On Jul 2, 2009, at 11:24 PM, David Winsemius wrote: On Jul 2, 2009, at 10:20 PM, Ivo Shterev wrote: a = list(diag(3), diag(3), diag(3)) a = list(diag(3), diag(3), diag(3)) sum(unlist(a)) [1] 9 Also: sum(sapply(a,I)) [1] 9 David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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.