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]]
Hi, You can follow this example: test <- structure(list(V1 = c(0L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), V2 = c(12L, 10L, 4L, 6L, 7L, 13L, 21L, 23L, 20L, 18L, 17L, 16L, 27L, 33L, 11L, 8L, 19L, 16L, 9L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA,-19L)) tapply(grid1$V2, grid1$V1, sum) 0 1 2 3 4 12 40 64 111 63 HTH Pascal Le 04/12/2012 16:59, T Bal a ?crit :> 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. >
Hi, T. Bal, homework? Take a look at ?tapply Regards -- Gerrit On Tue, 4 Dec 2012, T Bal wrote:> 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.
On 04-12-2012, at 08:59, T Bal wrote:> 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.Using Pascal's test object you can also use aggregate aggregate(test$V2,by=list(test$V1),FUN=sum) Berend
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 > res0 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.
HI, You can use either ?tapply(), ?aggregate(), ?ddply() from library(plyr) dat1<-read.table(text=" 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 ",sep="",header=FALSE) ?with(dat1,aggregate(dat1[,2],by=list(V1=dat1[,1]),sum)) ?# V1?? x #1? 0? 12 #2? 1? 40 #3? 2? 64 #4? 3 111 #5? 4? 63 A.K. ----- Original Message ----- From: T Bal <studenttbal at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, December 4, 2012 2:59 AM 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.