[R] Yearly hourly mean and NA

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Wed Jun 3 21:47:35 CEST 2020


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.



More information about the R-help mailing list