[R] Merge of the rows by finding the sum of the rows

Rui Barradas ruipbarradas at sapo.pt
Thu Oct 23 11:46:40 CEST 2014


Hello,

Try

aggregate(Rain ~ Year + Month, data = dat, FUN = sum)


Hope this helps,

Rui Barradas

Em 23-10-2014 01:29, Hafizuddin Arshad escreveu:
> Dear R users,
>
> Can someone help me on this?  I would like to find the sum of the Rain if
> the Month appears more than once. For example in row 3 and 4, October
> appear more than once, so I want to find the sum of the two rows and
> replace it so that the Month just appear once. It some sort of merge but by
> finding the sum for the third column. This is my data:
>
> 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), 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), 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)), .Names = c("Year",
> "Month", "Rain"), row.names = c(NA, 50L), class = "data.frame")
>
> Thank you so much for the help.
>
>
> 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