[R] question about sum of (column) elements in R
Jose Iparraguirre
Jose.Iparraguirre at ageuk.org.uk
Tue Dec 4 11:44:18 CET 2012
Hi,
Imagine the data you have is in a data frame, c, with columns a and b.
Then you can do this:
> res=tapply(b,c[,-2],sum)
> res[is.na(res)]<-0
> res
0 1 2 3 4
12 40 64 111 63
Hope it helps,
José
José Iparraguirre
Chief Economist
Age UK
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of T Bal
Sent: 04 December 2012 07:59
To: r-help at r-project.org
Subject: [R] question about sum of (column) elements in R
Hi,
I have the following data:
0 12
1 10
1 4
1 6
1 7
1 13
2 21
2 23
2 20
3 18
3 17
3 16
3 27
3 33
4 11
4 8
4 19
4 16
4 9
In this data file I would like to sum the numbers of second column which
belong to the same number in the first column.
So the output would be:
0 12
1 40
2 64
3 111
etc.
Thank you.
Kind regards,
T. Bal
[[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.
A Star for Christmas
Kick start the festive season by attending one of Age UK’s Carol Concerts, A Star for Christmas. Taking place at Manchester Cathedral on Saturday 1 December and London’s St Pancras Church (opposite Euston Station) on Thursday 6 December, they will feature special musical performances, readings by your favourite celebrities and carols, followed by mince pies and wine.
Tickets are priced at £20 full price/ £10 concessions. For more information,
please visit http://www.ageuk.org.uk/astarforchristmas or contact the Fundraising Events Team on
020 303 31725.
Age UK Improving later life
www.ageuk.org.uk
-------------------------------
Age UK is a registered charity and company limited by guarantee, (registered charity number 1128267, registered company number 6825798).
Registered office: Tavis House, 1-6 Tavistock Square, London WC1H 9NA.
For the purposes of promoting Age UK Insurance, Age UK is an Appointed Representative of Age UK Enterprises Limited, Age UK is an Introducer
Appointed Representative of JLT Benefit Solutions Limited and Simplyhealth Access for the purposes of introducing potential annuity and health
cash plans customers respectively. Age UK Enterprises Limited, JLT Benefit Solutions Limited and Simplyhealth Access are all authorised and
regulated by the Financial Services Authority.
------------------------------
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are
addressed. If you receive a message in error, please advise the sender and delete immediately.
Except where this email is sent in the usual course of our business, any opinions expressed in this email are those of the author and do not
necessarily reflect the opinions of Age UK or its subsidiaries and associated companies. Age UK monitors all e-mail transmissions passing
through its network and may block or modify mails which are deemed to be unsuitable.
Age Concern England (charity number 261794) and Help the Aged (charity number 272786) and their trading and other associated companies merged
on 1st April 2009. Together they have formed the Age UK Group, dedicated to improving the lives of people in later life. The three national
Age Concerns in Scotland, Northern Ireland and Wales have also merged with Help the Aged in these nations to form three registered charities:
Age Scotland, Age NI, Age Cymru.
More information about the R-help
mailing list