[R] adding up elements within a list

Martin Hughes M.Hughes at bath.ac.uk
Tue May 17 18:13:06 CEST 2011



Dear R users

I have a list, as follows:

> intvl.period.myrs
$Devonian
[1] 4.8 4.2 9.5 5.7

$Ordovician
[1]  7.2  5.1 10.2  1.9

$Silurian
[1] 4.7 3.0 7.8 2.0 3.3 1.6 2.6 2.7


I want to write a loop that will sum up the values in each part, and give me a
vector containing the (in this case 3) summed values

this is what I have so far:

		for (i in 1:length(names(intvl.periods.myrs)) {
			my.new.vector<-sum(intvl.periods.myrs[i] }

This does not work and probably for obvious reasons.

Thanks
Martin






-- 
Martin Hughes
MPhil/PhD Research in Biology
Rm 1.07,  4south
University of Bath
Department of Biology and Biochemistry
Claverton
Bath    BA2 7AY
Tel: 01225 385 437
M.Hughes at bath.ac.uk
http://www.bath.ac.uk/bio-sci/biodiversity-lab/hughes.html



More information about the R-help mailing list