bic ton
bic ton

Reputation: 1408

How to compute daily average for 15 years?

I have a daily data for 15 years from 1993 to 2008. I would like to compute the daily average, for the variable Open in the file, for each day of the year, based on a 31 day Window centred on the day of interest. Thus, 15⨯31 = 465 dates contribute to the statistics of one day.

Output is just 365 values out of the 15 years

The file can be downloaded from here: http://chart.yahoo.com/table.csv?s=sbux&a=2&b=01&c=1993&d=2&e=01&f=2008&g=d&q=q&y=0&z=sbux&x=.csv and can be read:

 df = read.csv("C:/data/table.csv", header = TRUE, stringsAsFactors = FALSE)

Upvotes: 2

Views: 176

Answers (1)

danas.zuokas
danas.zuokas

Reputation: 4643

library(data.table)

df <- as.data.table(read.csv('http://chart.yahoo.com/table.csv?s=sbux&a=2&b=01&c=1993&d=2&e=01&f=2008&g=d&q=q&y=0&z=sbux&x=.csv',header = TRUE, stringsAsFactors = FALSE))

df[, MAOpen := filter(Open, rep(1, 31) / 31)]
df[, Date := as.POSIXct(Date)]
df[, YDay := yday(Date)]

df[, mean(MAOpen), by = YDay]

Upvotes: 1

Related Questions