Hi Ani, Before the for loops add the following line when[,-1] <- when[,-1]%%12 + 1
This changes the month numbering so that Dec becomes 1, Jan becomes 2, etc Then your for loop does what you want. HTH, Eric On Wed, Apr 21, 2021 at 11:09 AM ani jaya <gaaa...@gmail.com> wrote: > Dear r community, > > I have a data frame that shows the month of occurrence of the maximum > value on let say 10 stations in 30 years. I want to make percentages > based on seasons (DJF, MAM, JJA, and SON). I can do that by the code > below but only when we put season by JFM, AMJ, ...(or 123; 456; > 789;..). > I believe it is not that hard to put 12 in front of 1 but my mind stuck. > > If someone can help me, I would be appreciated. > > Thank you in advance > > Ani > > > freqw<-matrix(NA, 4,10) > for(j in 1:4){ > for(i in 1:10){ > freqw[j,i]<-sum(table(when[i+1])[(j*3-2):(j*3)],na.rm=T) > } > } > freqw<-freqw/30*100 > > > > dput(when) > structure(list(Group.1 = structure(1:30, .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"), V1 = c(8L, > 1L, 1L, 12L, 12L, 1L, 4L, 1L, 2L, 3L, 7L, 4L, 1L, 6L, 2L, 2L, > 11L, 4L, 2L, 2L, 12L, 6L, 2L, 2L, 6L, 3L, 6L, 4L, 7L, 1L), V2 = c(1L, > 1L, 1L, 1L, 12L, 12L, 4L, 1L, 2L, 2L, 4L, 1L, 11L, 1L, 1L, 3L, > 3L, 1L, 12L, 2L, 5L, 2L, 12L, 2L, 1L, 5L, 10L, 4L, 12L, 1L), > V3 = c(4L, 1L, 12L, 12L, 2L, 4L, 1L, 5L, 1L, 1L, 2L, 2L, > 1L, 1L, 2L, 2L, 4L, 1L, 2L, 12L, 3L, 2L, 12L, 4L, 6L, 4L, > 2L, 3L, 5L, 1L), V4 = c(1L, 12L, 12L, 3L, 2L, 12L, 7L, 2L, > 12L, 1L, 12L, 2L, 1L, 12L, 1L, 10L, 7L, 1L, 1L, 3L, 10L, > 12L, 12L, 12L, 5L, 12L, 3L, 2L, 12L, 1L), V5 = c(1L, 12L, > 12L, 3L, 2L, 12L, 7L, 2L, 12L, 3L, 4L, 12L, 5L, 2L, 1L, 1L, > 2L, 5L, 2L, 2L, 3L, 2L, 10L, 3L, 1L, 1L, 4L, 1L, 4L, 6L), > V6 = c(12L, 1L, 12L, 2L, 1L, 12L, 3L, 1L, 4L, 12L, 6L, 12L, > 1L, 7L, 2L, 2L, 1L, 1L, 12L, 12L, 12L, 3L, 12L, 2L, 1L, 9L, > 12L, 1L, 4L, 12L), V7 = c(5L, 2L, 6L, 12L, 3L, 4L, 12L, 12L, > 6L, 2L, 11L, 12L, 2L, 5L, 11L, 4L, 3L, 4L, 1L, 12L, 10L, > 3L, 10L, 3L, 3L, 9L, 1L, 3L, 12L, 11L), V8 = c(4L, 5L, 5L, > 8L, 7L, 12L, 1L, 3L, 2L, 5L, 2L, 4L, 1L, 4L, 1L, 6L, 6L, > 2L, 3L, 5L, 4L, 1L, 3L, 12L, 4L, 12L, 12L, 3L, 6L, 7L), V9 = c(3L, > 2L, 3L, 11L, 7L, 2L, 4L, 4L, 5L, 6L, 6L, 3L, 4L, 6L, 1L, > 9L, 9L, 12L, 3L, 12L, 5L, 6L, 3L, 7L, 4L, 8L, 2L, 2L, 6L, > 6L), V10 = c(5L, 10L, 3L, 6L, 6L, 5L, 5L, 6L, 5L, 6L, 12L, > 6L, 7L, 2L, 1L, 6L, 4L, 5L, 4L, 2L, 7L, 6L, 7L, 7L, 2L, 7L, > 3L, 6L, 7L, 6L)), row.names = c(NA, -30L), class = "data.frame") > > ______________________________________________ > R-help@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.