[R] adding up elements within a list

Martin Hughes M.Hughes at bath.ac.uk
Tue May 17 19:44:50 CEST 2011


Brilliant!

Thanks

Martin

Quoting Henrique Dallazuanna <wwwhsd at gmail.com>:

> Try this:
>
> sapply(intvl.period.myrs, sum)
>
> On Tue, May 17, 2011 at 1:13 PM, Martin Hughes <M.Hughes at bath.ac.uk> wrote:
>>
>>
>> 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
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
>



-- 
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