[R] Query about calculating the monthly average of daily data columns
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Sep 12 21:13:43 CEST 2019
Hello,
Inline.
Às 17:33 de 12/09/19, Bert Gunter escreveu:
> But she wants *monthly* averages, Rui.
Thanks, my mistake.
Ergo ave() or tidyData
> equivalent, right?
Maybe. But ave() returns as many values as the input length, this seems
more suited for tapply or aggregate.
I will first create an example data set.
set.seed(1234)
start <- as.Date("03-01-1994", "%d-%m-%Y")
end <- as.Date("29-12-2000", "%d-%m-%Y")
date <- seq(start, end, by = "day")
date <- date[as.integer(format(date, "%u")) %in% 1:5]
df1 <- data.frame(date,
CountryA = rnorm(length(date)),
CountryB = rnorm(length(date)))
Now the averages by month
month <- zoo::as.yearmon(df1[[1]])
aggA <- aggregate(CountryA ~ month, df1, mean)
aggB <- aggregate(CountryB ~ month, df1, mean)
MonthReturns <- merge(aggA, aggB)
head(MonthReturns)
Final clean up.
rm(date, month, aggA, aggB)
Hope this helps,
Rui Barradas
>
> -- Bert
>
> On Thu, Sep 12, 2019 at 8:41 AM Rui Barradas <ruipbarradas using sapo.pt
> <mailto:ruipbarradas using sapo.pt>> wrote:
>
> Hello,
>
> Please include data, say
>
> dput(head(data, 20)) # post the output of this
>
>
> But, is the problem as simple as
>
> rowMeans(data[2:3], na.rm = TRUE)
>
> ?
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 15:53 de 12/09/19, Subhamitra Patra escreveu:
> > Dear R-users,
> >
> > I have daily data from 03-01-1994 to 29-12-2000. In my datafile,
> he first
> > column is date and the second and third columns are the returns
> of the
> > country A, and B. Here, the date column is same for both
> countries. I want
> > to calculate the monthly average of both country's returns by
> using a loop,
> > and then, I want to export the results into excel.
> >
> > Please help me in this regard.
> >
> > Please find the attached datasheet.
> >
> > Thank you.
> >
>
> ______________________________________________
> R-help using r-project.org <mailto: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