[R] Yearly hourly mean and NA

Ogbos Okike g||ted|||e2014 @end|ng |rom gm@||@com
Thu Jun 4 08:49:59 CEST 2020


Dear Jeff,
Yes. It worked. But I am still fine turning the script.
Please permit me to ask again.

Since I can handle one year. I wish to take on more years. I added extra
two years and now have 2000 to 2002.

I wish to plot the same graph for those years such that the x-axis will be
showing 2000, 2001 and 2002.  I tried to manipulate replicate function with
times and length.
I have a result but it does seem like what I am looking for. For each year,
I should get a similar graph. So the three years will just look like
similar plot repeated with some small differences. When I did per year and
added, I got it. But I could not add the error bars by doing them
separately for each year.

Here is part of my adjusted code:
d<-read.table("3years",col.names=c("y","m","d","h","count"))
y<-d$count
 data<-(y-mean(y,na.rm=TRUE))/mean(y,na.rm=TRUE)*100
A<-matrix(rep(1:24,366))
A1<-matrix(rep(25:49,365))
 A2<-matrix(rep(50:72,365))
A<-matrix(c(A,A1,A2),ncol=1)
I have also attached the complete code and data for the three years.
I am thanking you for any pointer.
Best regards
Ogbos

On Thu, Jun 4, 2020 at 3:14 AM Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
wrote:

> Perhaps read ?mean...
>
> On June 3, 2020 6:15:11 PM PDT, Ogbos Okike <giftedlife2014 using gmail.com>
> wrote:
> >Dear Jeff,
> >Thank you so much for your time.
> >I tried your code. It successfully assigned NA to the zeros.
> >
> >But the main code seems not to work with the NAs. The mean, for
> >example,
> >resulted in NA. I am attaching the data for a period of one year  and
> >the
> >code which I use  in plotting the data. Maybe it might be easier for
> >you to
> >spot where I run into error (my plot was just empty).
> >Thanks again.
> >Best regards
> >Ogbos
> >
> >
> >On Wed, Jun 3, 2020 at 8:47 PM Jeff Newmiller
> ><jdnewmil using dcn.davis.ca.us>
> >wrote:
> >
> >> df[[ 5 ]][ 0 == df[[ 5 ]] ] <- NA
> >>
> >> On June 3, 2020 1:59:06 AM PDT, Ogbos Okike
> ><giftedlife2014 using gmail.com>
> >> wrote:
> >> >Dear R-Experts,
> >> >I have a cosmic ray data that span several years. The data frame is
> >of
> >> >the
> >> >form:
> >> >03 01 01 00    3809
> >> >03 01 01 01    3771
> >> >03 01 01 02    3743
> >> >03 01 01 03    3747
> >> >03 01 01 04    3737
> >> >03 01 01 05    3751
> >> >03 01 01 06    3733
> >> >03 01 01 07    3732.
> >> >where the columns 1 to 5 stand for year, month, day, hour and
> >counts.
> >> >Some hours when the station does not have data are assigned zero,
> >> >implying
> >> >there could be several zeros in column 5. Since my aim is to plot
> >the
> >> >hourly mean for all the  years, I started learning with one year -
> >year
> >> >2003.
> >> >
> >> >I carefully went through the data, removing any day that contains
> >zero
> >> >for
> >> >any of the hours.  Instead of the 365 days in the year 2003, I ended
> >up
> >> >with 362 days.
> >> >
> >> >I saved that as CLMX1C (now stored in Ogbos2 with dput function, see
> >> >attached please).
> >> >
> >> >If I run the data with my script, it gives me what I am expecting.
> >My
> >> >script is:
> >> >d<-read.table("CLMX1C",col.names=c("h","count"))
> >> >y<-d$count
> >> >data<-(y-mean(y))/mean(y)*100
> >> >
> >> >A<-matrix(rep(1:24,362))
> >> >B<-matrix(data)
> >> >
> >> > oodf<-data.frame(A,B)
> >> > oodf<-data.frame(A,B)
> >> >library(plotrix)
> >> >std.error<-function(x) return(sd(x)/(sum(!is.na(x))))
> >> >oomean<-as.vector(by(oodf$B,oodf$A,mean))
> >> >oose<-as.vector(by(oodf$B,oodf$A,std.error))
> >> >plot(1:24,oomean,type="b",ylim=c(-0.4,0.5),
> >> > xlab="Hours",ylab="CR count",main="CR daily variation for 2004")
> >> >dispersion(1:24,oomean,oose,arrow.cap=.01)
> >> >
> >> >Now, instead of foraging through the big data removing the day for
> >> >which
> >> >there is a missing data for any hour, I wish to try to replace the
> >> >missing
> >> >data with NA and hoping that it will do the job for me.
> >> >
> >> >I added just three lines in the script above:
> >> >d<-read.table("2003",col.names=c("y","m","d","h","count"))
> >> >y<-d$count
> >> >df<-data.frame(y)#line 1
> >> >library('dplyr') # line 2
> >> >y<-na_if(df, 0) #line 3
> >> >data<-(y-mean(y))/mean(y)*100.
> >> >Then I started getting error messages:
> >> >Error in is.data.frame(x) :
> >> >  (list) object cannot be coerced to type 'double'
> >> >In addition: There were 26 warnings (use warnings() to see them).
> >> >
> >> >I hope you will assist me to deal with the issues of replacing zeros
> >> >with
> >> >NA in column 5 in such a way that my code will run.
> >> >
> >> >Iam ever indebted!!
> >> >Best regards
> >> >Ogbos
> >> >______________________________________________
> >> >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.
> >>
> >> --
> >> Sent from my phone. Please excuse my brevity.
> >>
>
> --
> Sent from my phone. Please excuse my brevity.
>


More information about the R-help mailing list