# [R] Challenge extracting months

Jim Lemon drjimlemon at gmail.com
Thu Feb 2 00:08:49 CET 2017

```Hi Kwesi,
It looks to me as though you have plotted the output of your data.
What you have used to plot it is a mystery. Maybe a stacked barplot
with horizontal=TRUE? I don't suppose that the matrix of input values
is available. Let's see, you have 20 rectangles in each bar and 36
bars. Suppose we have a matrix like this:

kqmat<-
matrix(sin(seq(pi/10,72*pi,pi/10))+rnorm(720,0,0.1),
ncol=20,byrow=TRUE)
library(plotrix)
color2D.matplot(kqmat,axes=FALSE,xlab="Rolling quarter",ylab="Year",
cellcolors=matrix(color.scale(kqmat,c(1,0,0),c(0,1,0),c(0,0,1)),nrow=20))
axis(2,at=1:36,labels=2015:1980,las=1)

This is a "plot in the dark", but it may enlighten.

Jim

On Thu, Feb 2, 2017 at 5:26 AM, Kwesi Quagraine <starskykwesi at gmail.com> wrote:
> Hello Jim, Hello everyone, does anyone know why this is happening? Any
> suggestions what might be causing it? I will be grateful for any help.
>
> Kwesi
>
> On Wed, Feb 1, 2017 at 1:12 AM, Jim Lemon <drjimlemon at gmail.com> wrote:
>>
>> Hi Kwesi,
>> I worked through your code below, and I think that when you have the
>> two variables "mon.t1" and "seas.t1" you can select a "rolling
>> quarter" like this:
>>
>> # the file name in your example is different from the one you sent
>>  skip=1,dec = ".")
>> era.nodes<-paste(era[,1],era[,2],sep=".")
>> era.nodes<-as.numeric(era.nodes)
>> era.nodes.days<-zooreg(era.nodes,start=as.Date("1980-01-01"),
>>  end=as.Date("2016-12-31"))
>> era.nodes.days.t1<-window(era.nodes.days,start=as.Date("1980-01-01"),
>>  end=as.Date("2016-12-31"))
>> mon.t1<-as.numeric(format(index(era.nodes.days.t1),"%m"))
>> # this loop transforms mon.t1 into an increasing sequence of months
>> for(i in 2:length(mon.t1)) {
>> }
>> for(i in 1:(max(mon.t1)-2)) {
>>  # this gives a logical index for the rolling quarter
>>  rq<-mon.t1 %in% i:(i+2)
>> }
>>
>> Each successive "rq" produced by the last loop can be used to extract
>> whatever values you want from "era" or "era.nodes".
>>
>> Jim
>>
>>
>> On Tue, Jan 31, 2017 at 9:04 PM, Kwesi Quagraine <starskykwesi at gmail.com>
>> wrote:
>> > Hello Jim, thanks for the code. But I come to you once again, I am not
>> > looking to do a rolling mean, but to select JFM,FMA,MAM etc from the
>> > data
>> > attached. Below is my sample code which actually selects these months. I
>> > will rather be glad if I can have a function that does the selection for
>> > all
>> > these 3 months selected for each year as shown in my last two lines of
>> > code;
>> > Taking into accounts years with 29 days in February etc.
>> >
>> > rm(list = ls())
>> > library(zoo)
>> > library(PCICt)
>> > library(lattice)
>> > library(RColorBrewer)
>> >
>> > setwd('/home/kwesi/Documents/700hpa/soms/')
>> > # Reading the data
>> >
>> > =
>> > "",skip=1,dec = ".")
>> > era.nodes      <- paste(era[,1],era[,2],sep=".")
>> >
>> > era.nodes      <-as.numeric(era.nodes)
>> >
>> > era.nodes.days<-zooreg(era.nodes,start=as.Date("1980-01-01"),end=as.Date("2016-12-31"))
>> >
>> >
>> > era.nodes.days.t1<-window(era.nodes.days,start=as.Date("1980-01-01"),end=as.Date("2016-12-31"))
>> >
>> > mon.t1<-as.numeric(format(index(era.nodes.days.t1),"%m"))
>> > seas.t1 <-as.numeric(format(index(era.nodes.days.t1),"%Y"))
>> > era.nodes.days.t1<-cbind(era.nodes.days.t1,mon.t1,seas.t1)
>> > era.nodes.days.t1
>> >
>> > jfm80<-era.nodes.days.t1[1:91,1:3[era.nodes.days.t1[1:91,2]==1|era.nodes.days.t1[1:91,2]==2|era.nodes.days.t1[1:91,2]==3]
>> > fma80<-era.nodes.days.t1[32:(91+30),1:3
>> >
>> > [era.nodes.days.t1[1:91,2]==2|era.nodes.days.t1[1:91,2]==3|era.nodes.days.t1[1:91,2]==4]
>> >
>> > On Tue, Jan 31, 2017 at 5:23 AM, Jim Lemon <drjimlemon at gmail.com> wrote:
>> >>
>> >> Hi Kwesi,
>> >> A mistake in the last email. Don't try to replace the column in
>> >> era.sta as the result will be a different length. Try this:
>> >>
>> >>
>> >> Jim
>> >>
>> >> On Tue, Jan 31, 2017 at 10:32 AM, Jim Lemon <drjimlemon at gmail.com>
>> >> wrote:
>> >> > Hi Kwesi,
>> >> > The function collapse_values will only work on a vector of numbers
>> >> > with FUN="mean". era.sta looks like a data frame with at least two
>> >> > elements. As the second of these elements seems to be numeric,
>> >> > perhaps
>> >> > this will work:
>> >> >
>> >> > era.sta[,2]<-collapse.values(era.sta[,2],3)
>> >> >
>> >> > Don't try to apply the names to era.sta, that was just something to
>> >> > make the example easier to understand. If you want to collapse more
>> >> > than one column of era.sta do each one at a time and assign them to a
>> >> > new data frame. In particular, if era[,1] is a vector of month names,
>> >> > you will have to create a new vector of quarter (three month) names.
>> >> > If there are very many of these, the collapse_values function can be
>> >> > modified to do it automatically.
>> >> >
>> >> > Jim
>> >> >
>> >> >
>> >> >
>> >> > On Tue, Jan 31, 2017 at 9:50 AM, Kwesi Quagraine
>> >> > <starskykwesi at gmail.com> wrote:
>> >> >> Hello Jim,this is my script now; I am having this error when I
>> >> >> called
>> >> >> the
>> >> >> function;" In mean.default(list(era...1. = 1:444, Node_freq =
>> >> >> c(-0.389855332400718,  :  argument is not numeric or logical:
>> >> >> returning
>> >> >> NA"
>> >> >> Any help will be much appreciated.
>> >> >>
>> >> >> Kwesi
>> >> >>
>> >> >> rm(list = ls())
>> >> >> setwd('/home/kwesi/Documents/700hpa/soms/')
>> >> >> # Reading the data
>> >> >>
>> >> >> ",",dec
>> >> >> =
>> >> >> ".")
>> >> >> era.scaled <- scale(era[,2:3], center = TRUE, scale = TRUE)
>> >> >> era.sta<-data.frame(era[,1],era.scaled)
>> >> >> era.sta
>> >> >>
>> >> >> collapse_values<-function(x,span,FUN="mean",na.rm=FALSE) {
>> >> >>   jump<-span-1
>> >> >>   newx<-rep(NA,length(x)-jump)
>> >> >>   for(i in 1:length(newx))
>> >> >>     newx[i]<-do.call(FUN,list(x[i:(i+jump)],na.rm=na.rm))
>> >> >>   return(newx)
>> >> >> }
>> >> >>
>> >> >> #test<-1:12
>> >> >> names(era.sta)<-month.abb
>> >> >> collapse_values(era.sta,3)
>> >> >> era.sta
>> >> >>
>> >> >>
>> >> >> On Mon, Jan 30, 2017 at 11:53 PM, Jim Lemon <drjimlemon at gmail.com>
>> >> >> wrote:
>> >> >>>
>> >> >>> Hi Kwesi,
>> >> >>> Even without the data, it seems clear that you want something like
>> >> >>> a
>> >> >>> rolling mean. Here is a simple function that will apply a function
>> >> >>> like "mean" to successive bits of a vector of numbers:
>> >> >>>
>> >> >>> collapse_values<-function(x,span,FUN="mean",na.rm=FALSE) {
>> >> >>>  jump<-span-1
>> >> >>>  newx<-rep(NA,length(x)-jump)
>> >> >>>  for(i in 1:length(newx))
>> >> >>>   newx[i]<-do.call(FUN,list(x[i:(i+jump)],na.rm=na.rm))
>> >> >>>  return(newx)
>> >> >>> }
>> >> >>>
>> >> >>> test<-1:12
>> >> >>> names(test)<-month.abb
>> >> >>> test
>> >> >>> collapse_values(test,3)
>> >> >>>  [1]  2  3  4  5  6  7  8  9 10 11
>> >> >>>
>> >> >>> Jim
>> >> >>>
>> >> >>>
>> >> >>>
>> >> >>> On Mon, Jan 30, 2017 at 11:53 PM, Kwesi Quagraine
>> >> >>> <starskykwesi at gmail.com> wrote:
>> >> >>> > Hello, I have a data with two variables nodes and index, I want
>> >> >>> > to
>> >> >>> > extract
>> >> >>> > 3 months seasons, with a shift of 1 month, that is, DJF, JFM, FMA
>> >> >>> > etc to
>> >> >>> > OND. Was wondering how to go about it. Kindly find attached the
>> >> >>> > data
>> >> >>> > as
>> >> >>> > csv.
>> >> >>> > Any help will be appreciated.
>> >> >>> >
>> >> >>> > Regards,
>> >> >>> > Kwesi
>> >> >>> >
>> >> >>> > --
>> >> >>> > Try not to become a man of success but rather a man of
>> >> >>> > value-Albert
>> >> >>> > Einstein
>> >> >>> >
>> >> >>> > University of Cape Coast|College of Agriculture and Natural
>> >> >>> > Sciences|Department
>> >> >>> > of Physics|
>> >> >>> > Team Leader|Recycle Up! Ghana|Technology Without Borders|
>> >> >>> > Other emails: kwesi.quagraine at ucc.edu.gh|kwesi.quagraine at teog.de|
>> >> >>> > Mobile: +233266173582
>> >> >>> > Skype: quagraine_cwasi
>> >> >>> > Twitter: @Pkdilly
>> >> >>> > ______________________________________________
>> >> >>> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> >> >>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> >> >>> > http://www.R-project.org/posting-guide.html
>> >> >>> > and provide commented, minimal, self-contained, reproducible
>> >> >>> > code.
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >> --
>> >> >> Try not to become a man of success but rather a man of value-Albert
>> >> >> Einstein
>> >> >>
>> >> >> University of Cape Coast|College of Agriculture and Natural
>> >> >> Sciences|Department of Physics|
>> >> >> Team Leader|Recycle Up! Ghana|Technology Without Borders|
>> >> >> Other emails: kwesi.quagraine at ucc.edu.gh|kwesi.quagraine at teog.de|
>> >> >> Mobile: +233266173582
>> >> >> Skype: quagraine_cwasi
>> >> >>
>> >
>> >
>> >
>> >
>> > --
>> > Try not to become a man of success but rather a man of value-Albert
>> > Einstein
>> >
>> > University of Cape Coast|College of Agriculture and Natural
>> > Sciences|Department of Physics|
>> > Team Leader|Recycle Up! Ghana|Technology Without Borders|
>> > Other emails: kwesi.quagraine at ucc.edu.gh|kwesi.quagraine at teog.de|
>> > Mobile: +233266173582
>> > Skype: quagraine_cwasi
>> >
>
>
>
>
> --
> Try not to become a man of success but rather a man of value-Albert Einstein
>
> University of Cape Coast|College of Agriculture and Natural
> Sciences|Department of Physics|
> Team Leader|Recycle Up! Ghana|Technology Without Borders|
> Other emails: kwesi.quagraine at ucc.edu.gh|kwesi.quagraine at teog.de|
> Mobile: +233266173582
> Skype: quagraine_cwasi