converting daily Total Returns and Prices to annual values in R

时间:2016-11-18 10:50:52

标签: r dataset time-series missing-data panel-data

I've data of Total Returns and Stock Prices on a daily basis for some banks, from 1997 to 2015, such that:

DATE         Bank1_TotalReturn Bank1_Price Bank2_TR Bank2_P  ... and so on for all other banks
01/01/1997         103.13           10.43       NA       NA
02/01/1997         104.66           11.12   153.89    23.08
03/01/1997            ...             ...      ...      ...
...and so on
for all other
days until
31/12/2015

Using R, I need to convert them in "medium annual values", so that I obtain a single annual medium value both for Total returns and Prices, for each bank, obviously, in order to make a Panel dataset.

nb: data contains lots of missing values, code must consider that issue! :)

1 个答案:

答案 0 :(得分:0)

您可以使用formatDATE中提取年份(一旦转换为Date课程),将日期转换为日期年份。然后使用dplyr

library(dplyr)
res <- df %>% group_by(Year=format(as.Date(DATE,format="%d/%m/%Y"),"%Y")) %>% 
              summarise_at(vars(-DATE), median, na.rm=TRUE)

我们首先group_by转换后的Year,然后我们使用summarise_at汇总除median之外的每列的DATE。请注意,我们将参数na.rm=TRUE传递给median以忽略NA

或使用aggregate

df$Year <- format(as.Date(df$DATE,format="%d/%m/%Y"),"%Y")
df <- df[,!(names(df)=="DATE")]
res <- aggregate(. ~ Year, data=df, FUN=median, na.rm=TRUE, na.action=NULL)

在这里,我们预处理dfDATE列更改为Year,我们使用公式. ~ Year指定聚合按Year分组的所有列。请注意,在aggregate中,我们指定na.action=NULL,因为默认na.action will remove rows that have any NA . Instead, we pass na.rm = TRUE to the function中位数to handle NA`

为了说明,我增加了您发布的数据:

df <- structure(list(DATE = c("01/01/1997", "02/01/1997", "03/01/1997", 
"04/01/1997", "01/01/1998", "02/01/1998", "03/01/1998", "04/01/1998"
), Bank1_TotalReturn = c(103.13, 104.66, 105.23, NA, 113.13, 
114.66, 115.23, NA), Bank1_Price = c(10.43, 11.12, 12.15, NA, 
11.43, 12.12, NA, 13.15), Bank2_TR = c(NA, 153.89, 145.89, 136.89, 
140.92, 153.89, 145.89, 146.89), Bank2_P = c(NA, 23.08, NA, NA, 
20.9, 23.08, 25.73, 25.98)), .Names = c("DATE", "Bank1_TotalReturn", 
"Bank1_Price", "Bank2_TR", "Bank2_P"), class = "data.frame", row.names = c(NA, 
-8L))
##        DATE Bank1_TotalReturn Bank1_Price Bank2_TR Bank2_P
##1 01/01/1997            103.13       10.43       NA      NA
##2 02/01/1997            104.66       11.12   153.89   23.08
##3 03/01/1997            105.23       12.15   145.89      NA
##4 04/01/1997                NA          NA   136.89      NA
##5 01/01/1998            113.13       11.43   140.92   20.90
##6 02/01/1998            114.66       12.12   153.89   23.08
##7 03/01/1998            115.23          NA   145.89   25.73
##8 04/01/1998                NA       13.15   146.89   25.98

使用这两种方法的数据,结果是:

print(res)
##   Year Bank1_TotalReturn Bank1_Price Bank2_TR Bank2_P
##1  1997            104.66       11.12   145.89  23.080
##2  1998            114.66       12.12   146.39  24.405

如果意图是计算均值而不是中位数,那么只需将mean替换为median。对于aggregate解决方案:

res <- aggregate(. ~ Year, data=df, FUN=mean, na.rm=TRUE, na.action=NULL)
print(res)
##  Year Bank1_TotalReturn Bank1_Price Bank2_TR Bank2_P
##1 1997            104.34    11.23333 145.5567 23.0800
##2 1998            114.34    12.23333 146.8975 23.9225