[R] Finding the Summation of Monthly Amount
peter dalgaard
pdalgd at gmail.com
Mon Oct 13 09:53:45 CEST 2014
Looks like a job for aggregate()
-pd
On 13 Oct 2014, at 04:25 , dila radi <dilaradi21 at gmail.com> wrote:
> Hi all,
>
> Could someone help me on this? I have this kind of data set
>
> structure(list(Year = c(1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
> 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
> 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
> 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
> 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
> 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
> 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
> 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
> 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
> 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
> 1972L, 1972L, 1972L, 1972L, 1973L, 1973L, 1973L, 1973L, 1973L,
> 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
> 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
> 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
> 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
> 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
> 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
> 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
> 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
> 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
> 1973L, 1973L, 1973L, 1973L), Month = c(1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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0, 0, 0, 0, 0, 50.7,
> 1.2, 2.5, 0, 0, 0, 0, 17, 0, 2.5, 0, 0, 0, 0, 3.8, 0, 0, 0, 0,
> 0, 0, 2.7, 2.7, 12.6, 0, 0, 0, 0, 0, 0, 0, 3.3, 0, 0, 5.5, 2.5,
> 0, 35.5, 5, 3.3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6.3,
> 0, 0, 0, 5.8, 0, 0, 0, 15.4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5.5,
> 30.2, 64.5, 0, 11.9, 3.8, 3.3, 7.1, 27.9, 32, 106.6, 0.2, 0,
> 5.3, 4.5, 0, 0, 1.5, 11.9, 6.6, 0, 0, 11.9, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 8.3, 8.1, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 30.9, 1.2,
> 0, 0, 9.3, 82.5, 16.5, 0, 0, 0, 0.7, 0, 0, 36, 0, 3.8, 0, 0,
> 0, 0, 0, 0, 0, 4.3, 0, 0, 38, 8.8, 0, 26.4, 0, 0, 0.7, 13.4,
> 9.6, 8.3, 0, 3.8, 8.8, 0, 45.7, 1.2, 12.9, 0, 0, 0, 21.3, 1,
> 0)), .Names = c("Year", "Month", "Rain"), class = "data.frame", row.names =
> c(NA,
> -271L))
>
> and I want to find the sum of Rain in column 3 according to their month.
> How could I achieved this so that it would appear as follow:
>
> structure(list(Year = c(1971L, 1971L, 1971L, 1972L, 1972L, 1972L,
> 1973L, 1973L, 1973L), Month = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
> 3L), Rain = c(176.8, 228.2, 90.9, 81, 73.1, 127.7, 242.8, 189.5,
> 204.2)), .Names = c("Year", "Month", "Rain"), class = "data.frame",
> row.names = c(NA,
> -9L))
>
> Thank you so much
>
> Dila
>
> [[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.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list