[R] Finding Sum

Hafizuddin Arshad
Sat, 25 Oct 2014 02:14:44 -0000

Hi,

I really need help on this. I have this data set:

structure(list(Year = c(1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
1973L, 1974L, 1974L, 1974L, 1974L, 1974L, 1974L, 1974L, 1974L,
1974L, 1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1976L,
1976L, 1976L, 1976L, 1976L, 1976L, 1976L, 1976L, 1976L, 1977L,
1977L, 1977L, 1977L, 1977L, 1977L, 1978L, 1978L, 1978L, 1978L,
1978L, 1978L, 1978L, 1979L, 1979L, 1979L, 1979L, 1979L, 1980L,
1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1981L, 1981L, 1981L,
1981L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1984L, 1984L,
1984L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1987L, 1987L,
1987L, 1987L, 1987L, 1987L, 1988L, 1988L, 1988L, 1988L, 1988L,
1988L, 1988L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L,
1990L, 1990L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L,
1991L, 1991L, 1991L, 1991L, 1992L, 1992L, 1993L, 1993L, 1993L,
1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L,
1994L, 1994L, 1994L, 1994L, 1995L, 1995L, 1995L, 1995L), Month = c(3L,
6L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 2L, 9L, 12L, 12L, 12L,
12L, 3L, 9L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 8L, 9L,
11L, 11L, 11L, 11L, 11L, 12L, 12L, 1L, 2L, 4L, 10L, 11L, 12L,
12L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 9L, 10L, 11L,
12L, 12L, 12L, 6L, 6L, 9L, 11L, 11L, 12L, 12L, 10L, 11L, 11L,
11L, 11L, 7L, 9L, 11L, 12L, 12L, 12L, 12L, 7L, 11L, 12L, 12L,
7L, 10L, 12L, 12L, 12L, 12L, 5L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 11L, 12L, 12L, 3L, 3L, 11L, 12L, 12L, 12L, 6L, 12L,
12L, 12L, 12L, 12L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 9L, 9L,
10L, 10L, 11L, 12L, 12L, 4L, 9L, 1L, 6L, 8L, 10L, 11L, 11L, 11L,
11L, 12L, 12L, 12L, 6L, 12L, 6L, 10L, 10L, 5L, 10L, 10L, 10L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 2L, 11L, 11L, 12L
), Rain = c(196, 88.8, 96, 70.6, 104.9, 80, 102.8, 161.5, 123.4,
70.8, 99, 77.7, 130.8, 134.1, 86.3, 213.3, 169.9, 89.4, 78.7,
81.5, 100.3, 107.1, 93.2, 83.8, 253.2, 75.4, 134.5, 84.5, 82.5,
82.5, 119.5, 119.5, 134.5, 83.5, 372.5, 79.5, 112, 80.5, 129.5,
120.5, 126, 73, 93.5, 86.5, 140.5, 76, 180.5, 75, 130.5, 130.5,
104, 110.5, 210.5, 70.5, 120.5, 120, 81, 84, 74.5, 82, 73.5,
177.5, 77.5, 88, 92.5, 93.5, 103, 93.5, 82, 85, 102.5, 76.5,
180, 144.5, 117, 73, 253.5, 99, 196, 105, 88.8, 71, 82.5, 191,
184.8, 72.5, 104.5, 107, 187, 141.5, 149.5, 115.5, 252, 199.5,
106.5, 192, 217.5, 112, 127, 87.5, 73, 75, 102, 102, 160, 172,
129, 150, 77, 120, 111, 190, 287.5, 120, 288, 183, 74, 77.5,
122, 112, 100, 140, 91, 158, 105, 100, 83, 75, 260, 80, 135,
218, 87, 85, 240, 115, 80, 76, 100.5, 82, 184, 72.5, 79, 152,
120, 128, 120, 154, 135, 141, 156, 132, 260, 73.5, 76.5, 129,
200, 70.5)), .Names = c("Year", "Month", "Rain"),
class = "data.frame",
row.names = c(NA,
-158L))


I want to find the sum of the Rain according to their Month regardless of
their Years. The problem with my data is the Month is not fixed to Jan-
Dec. For example I want to find the sum of Rain for January( if have) from
1971 to 1995 regardless of their years and so on. I have tried this:

## monthly_summary.2 code use monthly data with mean, var and sd
monthly_summary.2 <- function(dt)
{ mt1 <- matrix(dt[,3],nrow=12,ncol=82,byrow=F)
  mt <- t(mt1)
  colnames(mt) <-
c("Jan","Feb","Mar","Apr","May","June","July",

"Aug","Sept","Oct","Nov","Dec")
  mn <- colMeans(mt)
  vr <- diag(var(mt[,c(1:12)]))
  sd <- sqrt(vr)
  #rbind(mt,mn = mn,vr = vr,sd = sd)
  rbind(mn = mn,vr = vr,sd = sd)
}
## call function for monthly summary of data, mean, variance and std
ddt1.1 <- monthly_summary.2(dt1); round(ddt1.2,2) # kb

but R gives me this:

In matrix(dt[, 3], nrow = 12, ncol = 82, byrow = F) :
  data length [290] is not a sub-multiple or multiple of the number of rows
[12]

Can you help me on this? The output should be something like this:

structure(c(135.073170731707, 3766.29433303222, 61.3701420320356,
115.35, 1962.35981481481, 44.2985306168818, 131.704878048781,
3508.69256850346, 59.2342178854711, 118.080487804878, 2323.96677506775,
48.2075385709305, 138.373170731707, 4960.1308762421, 70.4281965993883,
115.891463414634, 2313.38375338753, 48.0976481066126, 132.14512195122,
4258.86522282445, 65.2599817868841, 112.348780487805, 1813.78573923517,
42.5885634793564, 130.989024390244, 4305.92740891298, 65.6195657476715,
109.289024390244, 1722.0859274315, 41.4980231749838, 131.365853658537,
3999.19635049684, 63.2391994770399, 109.503658536585, 1622.71838151159,
40.2829788063345), .Dim = c(3L, 12L), .Dimnames = list(c("mn",
"vr", "sd"), c("Jan", "Feb",
"Mar", "Apr", "May", "June",
"July",
"Aug", "Sept", "Oct", "Nov",
"Dec")))


Thank you.


Arshad

	[[alternative HTML version deleted]]