[R] Finding Rainfall Amount

Duncan Mackay dulcalma at bigpond.com
Wed Oct 8 14:03:41 CEST 2014


If you make a factor with year with levels including all the years that you
want then use tapply eg  with dat as the name of the data.frame
The same applies for Month but I have not done so

dat$Yr <- factor(dat$Year, levels = c(1971:1980))
with(dat, tapply(Rain,  list(Yr, Month), sum, na.rm=T))
         1    2     9    10    11    12
1971 138.5 14.1 138.3 144.4 139.8 172.7
1972  36.0   NA    NA    NA    NA    NA
1973    NA   NA    NA    NA    NA    NA
1974    NA   NA    NA    NA    NA    NA
1975    NA   NA    NA    NA    NA    NA
1976    NA   NA    NA    NA    NA    NA
1977    NA   NA    NA    NA    NA    NA
1978    NA   NA    NA    NA    NA    NA
1979    NA   NA    NA    NA    NA    NA
1980    NA   NA    NA    NA    NA    NA

regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Hafizuddin Arshad
Sent: Wednesday, 8 October 2014 12:26
To: r-help at r-project.org
Subject: [R] Finding Rainfall Amount

Dear R users,

I have this kind of data set which is part of my data:

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, 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), 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, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 28.3, 2, 0.1, 0.1, 0.1,
0.1, 0.1, 1.8, 0.2, 2, 0, 0)), .Names = c("Year", "Month", "Rain"
), class = "data.frame", row.names = c(NA, -194L))


I would like to find the rainfall total for each month within a year. The
result should be like in this form:

structure(list(V1 = c(28.4, 9.2, 48.8, 185.2, 4.1, 67.6, 49.7,
21.8, 5.6, 24.2, 0.8, 32, 88.9, 61.6, 36.8, 74, 16.3, 2.6, 7.6,
149.7, 13, 65.2, 3.8, 72.1, 49, 3.8, 24.1, 4.3, 19.6, 32.3, 5.6,
54.6, 94.2, 40.7, 101.3, 0.5, 11.7, 9.9, 86.4, 24.3, 91.3, 66.5,
13.2, 0.8, 16, 24.7, 18.1, 13.5, 126.5, 18.1, 62.5, 98.9, 212.6,
19.2, 42.4, 41.6, 73.2, 15, 43.2, 43, 1.4, 167.4, 11.4, 14, 15.6,
23.4, 23.8, 104.2, 58.8, 191.6, 37.2, 121, 36.2, 5.8, 26, 13,
18.6, 63.2, 28.6, 57.8), V2 = c(53.6, 0, 73.7, 106.8, 0, 9.2,
141.6, 32.3, 3.3, 28.5, 55.1, 0, 78.5, 71.6, 50.3, 1.3, 24.7,
201.2, 7.4, 59.9, 26.5, 46, 3.6, 5.6, 93.7, 56.1, 59.8, 33.7,
70.6, 38.9, 4.8, 18.6, 7.1, 62.2, 17.6, 17.5, 13.7, 42.2, 20.6,
18.3, 1.5, 17.1, 22.6, 2, 44.6, 8.2, 8.4, 69.6, 75.9, 59.6, 35.4,
122.9, 62.1, 43.8, 39.6, 111, 1.8, 10, 76.2, 0, 43.4, 194, 8.2,
9.8, 18.2, 5, 69.4, 1.6, 51.2, 51.2, 88.6, 43.4, 11.8, 19.6,
68.2, 48.6, 6.4, 102.4, 24.5, 73), V3 = c(1.3, 0.3, 73.4, 29.5,
58.6, 6.3, 119.7, 30, 6.3, 175.5, 66.3, 14.8, 24.2, 32.7, 28.9,
29.7, 1.6, 132.6, 10.9, 67.8, 23.7, 10.4, 53.6, 15.2, 82.4, 90.2,
3.8, 52.5, 81.2, 5.6, 67.8, 1, 3.3, 85.8, 138.6, 38, 47.8, 72.5,
5.9, 39.6, 16.5, 27.8, 39.5, 9.7, 58, 11.5, 14.8, 99.3, 48.1,
33.5, 14.3, 70.9, 17.5, 58.6, 20.8, 88.2, 72.8, 20.2, 32, 131,
52.4, 17.4, 46.6, 3.8, 35.2, 102.2, 10.6, 43.2, 33.4, 101.8,
43, 53.8, 42.8, 1.6, 45.4, 7.8, 1.2, 18, 39.4, 28), V4 = c(29.5,
0, 37.8, 9.1, 123.2, 1.8, 27.2, 44.5, 33.7, 73.2, 98.2, 29.5,
99.5, 117.7, 37.3, 19.9, 47.2, 185.8, 100, 21.9, 25, 80.5, 49.1,
7.4, 47.9, 30, 41.2, 32.8, 16.5, 88.9, 99.4, 43, 47.1, 10.2,
104.7, 21.6, 46, 89.8, 60.6, 60.4, 18.6, 24.4, 134.1, 20.9, 20.3,
2.6, 63.9, 83.8, 109.2, 54.6, 36, 134.9, 168, 24.6, 10, 14, 50.4,
35.4, 7.2, 31.6, 72.2, 38, 49.6, 36.2, 33.2, 172, 143.6, 10,
122, 6.4, 10.4, 29.4, 5.8, 118.8, 17.6, 26, 15.2, 18, 36.6, 18.6
), V5 = c(52.1, 68.4, 51.2, 20.6, 89.5, 58, 56, 37.7, 56.1, 153.7,
16.7, 58, 0, 23.7, 9.9, 40.6, 20.1, 29.7, 35.7, 4.3, 158.9, 12.2,
83.2, 28.5, 38.7, 28.7, 70.5, 38.6, 59, 100.2, 154.2, 61.7, 23.7,
67.3, 147.5, 17.5, 115.2, 12.4, 136.6, 16, 113.9, 103.5, 42.9,
48.3, 72.4, 33.6, 138.8, 67.8, 38.9, 61.8, 19.6, 51.9, 115.8,
63.8, 7.6, 78.4, 137, 30.6, 53, 55.8, 126.8, 9.2, 84.2, 61, 161.4,
111.4, 90.2, 11.2, 38.4, 32.2, 20.2, 39.8, 82, 14.6, 31.6, 36.8,
47.6, 4.6, 64.2, 24), V6 = c(65.9, 149.5, 69.4, 77.7, 102.8,
14.5, 58.1, 49.3, 37.9, 201.4, 99, 63.1, 40.9, 50.4, 105.9, 39.4,
89.1, 132, 14.2, 45.8, 104.6, 56.2, 22.6, 104.4, 58.5, 70.4,
40.8, 23.8, 31.5, 100.7, 144.4, 96.4, 45.4, 89.2, 127.5, 120.2,
18.8, 38.5, 21.3, 70.4, 94.3, 86.5, 80.5, 16.4, 77.5, 37.4, 58,
20.4, 42.5, 42.5, 11, 102.1, 19.3, 13.2, 25, 96.2, 64.8, 60.4,
170.6, 31.8, 66.8, 11.6, 68.2, 35.4, 75, 62.8, 81.2, 162.1, 55,
30.8, 101.2, 97.8, 24, 64.6, 46.2, 71, 103.3, 131.6, 33, 24.8
), V7 = c(72.6, 87.1, 34, 71.4, 62.8, 94.7, 59.2, 29, 54.7, 62.9,
54.1, 50.3, 100.4, 95, 142.2, 26.8, 32.5, 51.2, 40.7, 63.9, 43.8,
71.8, 29.2, 49.7, 145.8, 119.1, 29.7, 78.3, 69.7, 89.2, 73, 72,
45.4, 109.6, 128.8, 60.8, 122.4, 44.3, 139.5, 64.8, 46.5, 90.9,
165.8, 53.2, 57.4, 31.8, 61.6, 134.9, 42.9, 56.1, 46, 96.7, 149.6,
110.6, 43, 31.6, 119.6, 72, 157.6, 17, 101.6, 110.2, 48.6, 191.8,
106.6, 53.2, 154.4, 95.8, 42, 147.4, 52.2, 96.8, 24.6, 76.4,
29.4, 87.6, 71.9, 73.2, 104.3, 80), V8 = c(67.6, 59.2, 108.4,
35.9, 88.2, 91.3, 22.8, 49.6, 77.9, 67.6, 91.2, 48.9, 124.6,
54.6, 85.8, 62.8, 63.3, 174.7, 32.3, 22.7, 99.9, 123.2, 4.8,
149, 66, 73.9, 37.4, 22.4, 69.5, 87.3, 66.9, 87.2, 81.5, 109.1,
47.4, 22.8, 92.4, 60.9, 77.5, 91.3, 91.9, 78.2, 39.4, 107.5,
57.9, 65.5, 76, 47.2, 145.5, 95.9, 106, 120.4, 108, 96.8, 30.2,
16.6, 54, 63, 120.6, 10, 144.2, 126.4, 160.6, 88.2, 23.4, 83.8,
110.8, 99.8, 128.8, 35.1, 18.6, 84, 80.8, 78.6, 53.8, 142.2,
67.8, 126, 19.9, 49.8), V9 = c(58.7, 35.7, 50.7, 49.3, 38.9,
22.4, 28, 79.2, 24.9, 36.7, 34.5, 94.4, 51, 87.2, 11.4, 82, 25.2,
43.8, 50, 42.5, 51, 56.3, 8.4, 29.2, 5.4, 70.7, 48.1, 63.1, 84.5,
53, 82.4, 99.1, 30.6, 74.2, 74.6, 43, 71.2, 67, 111.5, 29, 51.2,
106, 132.9, 64.6, 114.2, 32.5, 21.7, 69.1, 126.1, 42.7, 15.3,
76.7, 79.6, 93.4, 93.8, 31.6, 129, 54.6, 57.8, 45.4, 82.2, 68.8,
31.6, 63.4, 61.2, 47, 49.6, 85.8, 133.4, 125.8, 24.8, 93.8, 67.8,
135, 50.2, 57.4, 50.6, 125.4, 10.8, 23.6), V10 = c(34.5, 55.1,
61.6, 17.6, 53.7, 89.5, 69.9, 28.8, 98.6, 47.8, 45.7, 30.1, 203,
107, 32.5, 73.6, 9.7, 84.9, 3.6, 32.7, 79.1, 35.5, 69.9, 81,
20, 85.3, 127.9, 214.7, 113.1, 83, 101.1, 59.1, 41.7, 145.7,
95.4, 50.5, 123.2, 82.6, 37.6, 41.8, 68, 82.6, 97, 51.2, 121.7,
46.1, 64.8, 54.2, 31, 38.9, 20.8, 106.7, 137.8, 180.8, 111.4,
16.2, 51, 63.6, 26.8, 9.2, 85, 74, 94.8, 131.4, 23.4, 70.8, 72.4,
8.6, 168, 126, 24.2, 58.2, 13.8, 40, 6.4, 85.5, 6.2, 107, 29.4,
12.2), V11 = c(0, 30.4, 125.3, 16.8, 16.6, 18, 5.3, 26.5, 42.5,
87.1, 33.3, 61.5, 151.9, 13.3, 13, 25, 5.1, 118.1, 41.6, 34.5,
49.5, 66.8, 14.3, 44.5, 103.5, 59.3, 74, 103.3, 61.5, 23, 73.5,
48.9, 158.7, 49, 32.1, 19.1, 50.5, 99.6, 47.5, 56.1, 32, 38.3,
40.7, 36.8, 63.9, 9.4, 49.9, 33.5, 101, 150.3, 36.9, 82.2, 39.4,
14.2, 45.4, 15.8, 88.4, 19.8, 22.2, 2.8, 37.4, 9.2, 62.6, 37.6,
81, 33.2, 9, 11.8, 96, 80.8, 52.8, 46.4, 49.2, 94.2, 15.2, 86.2,
100.6, 56.2, 90.6, 74.4), V12 = c(38.8, 86.8, 73.4, 0, 32.5,
36.3, 3.3, 44.2, 157.5, 1, 22.1, 179.7, 42, 37.1, 184.2, 31.5,
1.5, 3.8, 54.7, 19.3, 19.9, 0.5, 45.5, 15.8, 42.7, 104.7, 52.6,
15.4, 67.4, 21.7, 66.5, 11.8, 124.4, 39.3, 17.3, 77.5, 11.4,
18, 68.2, 77, 41.3, 16.3, 61.6, 24.6, 197.3, 4.6, 62.1, 24.4,
37.9, 97.7, 1.3, 69.8, 13.9, 98.4, 30.6, 8.6, 43.4, 102, 35,
21.8, 55.4, 18.2, 67.6, 47, 127.8, 28.2, 14, 67.8, 112, 136.6,
35.2, 35.8, 9.6, 13, 8.8, 119.6, 68.4, 45.8, 77, 56.6)), .Names = c("V1",
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11",
"V12"), class = "data.frame", row.names = c(NA, -80L))

where V1 until V12 as month and 1 until 80 as a years. How can I do this in
R? Thank you for helping me.


Arshad

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



More information about the R-help mailing list