[R] get a month where max value is

ani jaya g@@@uu| @end|ng |rom gm@||@com
Wed Dec 16 07:49:34 CET 2020


Duh.. I should know that..
That was excellent. Didn't realize that.
Need rest(or practice) for my brain maybe.

Thank you Jim...


On Wed, Dec 16, 2020 at 3:27 PM Jim Lemon <drjimlemon using gmail.com> wrote:
>
> Hi Ani,
> Not sure about this, but is this what you want?
>
> ann10<-aggregate(mon10[3:12],list(mon10$Group.1),which.max)
>
> Jim
>
> On Wed, Dec 16, 2020 at 4:55 PM ani jaya <gaaauul using gmail.com> wrote:
> >
> > Dear R-Help,
> >
> > I have a data frame containing monthly maxima of rainfall in 10
> > locations for 30 year and want to look at a month where an annual
> > maxima happens. I can get the annual maxima using aggregate. I try to
> > extract the month using the code below.
> >
> > ann10<-aggregate(mon10[3:12],list(mon10$Group.1),max,na.rm=T)
> > when<-matrix(NA,30,10)
> > dum<-list()
> > for(j in 1:10){
> >   for(i in 1:30){
> >     dum<-which(mon10[,j+2]==ann10[i,j+1])
> >     when[i,j]<-mon10$Group.2[dum[1]]
> >   }
> > }
> >
> > I am aware that the code above is wrong when multiple max value
> > detected (so I use dum[1] there). Any elegant solution how to find the
> > month?? Thank you
> >
> > dput(head(mon10,36))
> >
> > structure(list(Group.1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
> > 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> > 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("1985",
> > "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993",
> > "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001",
> > "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009",
> > "2010", "2011", "2012", "2013", "2014"), class = "factor"), Group.2 =
> > structure(c(1L,
> > 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L,
> > 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
> > 8L, 9L, 10L, 11L, 12L), .Label = c("01", "02", "03", "04", "05",
> > "06", "07", "08", "09", "10", "11", "12"), class = "factor"),
> >     V1 = c(0, 0, 0, 0, 0, 0, 1, 13, 0, 0, 0, 0, 175, 123, 76,
> >     112, 35, 52, 14, 28, 63, 68, 30, 67, 107, 46, 44, 22, 33,
> >     25, 10, 5, 50, 10, 32, 59), V2 = c(167, 50, 48, 114, 128,
> >     37, 54, 5, 23, 36, 10, 43, 175, 123, 110, 112, 58, 12, 18,
> >     171, 63, 68, 30, 79, 189, 40, 35, 45, 23, 10, 0, 1, 1, 5,
> >     34, 71), V3 = c(63.0054, 68.9842, 1, 115.9904, 5.796, 53.0929,
> >     63.0092, 18.3692, 0, 30.1854, 12.214, 55.0721, 91.9728, 64.9951,
> >     28.431, 21.6815, 38.4651, 48.1583, 1, 1, 1, 7.654, 50.1237,
> >     86.9713, 69.9766, 14.2489, 45.2282, 12.4218, 18.5532, 23.9453,
> >     38.4967, 0, 0, 1.8495, 41.3493, 69.98), V4 = c(112, 94, 70,
> >     18, 6.1, 55, 84.1, 7.1, 0, 4.1, 24.9, 26.9, 42, 38, 38, 62,
> >     26, 50, 8, 0, 6, 40, 18, 77, 30, 78, 31, 28, 13, 27, 50,
> >     0, 0, 0, 29, 84), V5 = c(114.2545, 96.2488, 71.9131, 14.299,
> >     22.2288, 55.4017, 1.971, 3.69, 0, 13.301, 57.5355, 27.3004,
> >     39.6649, 34.3766, 34.3278, 63.1532, 21.4376, 49.5373, 4.556,
> >     0, 2.7328, 36.8717, 66.3924, 79.1253, 25.1753, 80.1422, 26.2089,
> >     23.232, 9.4119, 22.3628, 49.4622, 0, 0, 0, 24.1889, 86.2128
> >     ), V6 = c(65, 35, 83, 46, 50, 26, 28, 10, 6, 13, 90, 97,
> >     178, 138, 79, 47, 30, 73, 5, 0, 1, 24, 55, 46, 80, 62, 56,
> >     57, 21, 18, 0, 0, 0, 9, 36, 175), V7 = c(110, 100, 100, 63,
> >     167, 40, 5, 7, 76, 0, 15, 17, 55.1, 116.1, 57.9, 21.1, 0.3,
> >     103.1, 14, 0, 5.1, 5.1, 1, 20.1, 31, 26.9, 0.5, 0, 27.9,
> >     41, 0, 6.1, 0, 0, 0, 0), V8 = c(78, 87, 37, 98, 0, 0, 0,
> >     0, 0, 0, 0, 0, 64, 90, 89, 73, 104, 70, 101, 15, 0, 0, 0,
> >     0, 0, 1, 38, 97, 114, 28, 0, 0, 0, 0, 0, 0), V9 = c(29, 17,
> >     72, 29, 16, 25, 23, 43, 37, 10, 15, 7, 14, 77, 54, 43, 22,
> >     58, 63, 27, 12, 12, 16, 9, 22, 33, 60, 54, 40, 37, 35, 12,
> >     1, 4, 11, 49), V10 = c(63.1161, 11.9425, 35.3226, 34.0615,
> >     92.9325, 34.0785, 39.009, 21.5103, 17.9209, 50.9037, 32.7218,
> >     52.2346, 63.1381, 99.946, 48.0861, 63.1813, 43.7723, 86.9132,
> >     39.1252, 49.6261, 40.7285, 112.9464, 49.8403, 66.5253, 21.5789,
> >     12.0566, 89.927, 71.7187, 37.816, 5.3817, 1, 0, 0, 4.636,
> >     40.7964, 88.9239)), row.names = c(1L, 31L, 61L, 91L, 121L,
> > 151L, 181L, 211L, 241L, 271L, 301L, 331L, 2L, 32L, 62L, 92L,
> > 122L, 152L, 182L, 212L, 242L, 272L, 302L, 332L, 3L, 33L, 63L,
> > 93L, 123L, 153L, 183L, 213L, 243L, 273L, 303L, 333L), class = "data.frame")
> >
> > sessionInfo()
> > R version 4.0.2 (2020-06-22)
> > Platform: x86_64-w64-mingw32/x64 (64-bit)
> > Running under: Windows 10 x64 (build 18363)
> >
> >
> > Best,
> > Ani
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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