[R] Occurrence by season

ani jaya g@@@uu| @end|ng |rom gm@||@com
Fri Apr 23 02:42:13 CEST 2021


Thank you very much Mr. Eric and Mr. Bert.
It was really helpful.


Best,
Ani

On Thu, Apr 22, 2021 at 3:36 AM Bert Gunter <bgunter.4567 using gmail.com> wrote:
>
> Simpler, and no loops needed per column:
> ## this makes the idea clear
> > x <- 1:12
> > (x %% 12) %/% 3 ## first modulo, then integer divide
>  [1] 0 0 1 1 1 2 2 2 3 3 3 0
>
> ## add 1 if you prefer 1 to 4. Better yet, probably, is to turn it into a factor:
> > f <- factor(c("win","spr","sum","fal")[x+1])
> > f
>  [1] win win spr spr spr sum sum sum fal fal fal win
> Levels: fal spr sum win
>
> ## see ?factor if you want to alter the ordering of the levels (e.g. for graphics, nice tables of results, etc.)
>
> Cheers,
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Wed, Apr 21, 2021 at 1:48 AM Eric Berger <ericjberger using gmail.com> wrote:
>>
>> 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 <gaaauul using 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 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.
>> >
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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