test_year <- function(data){
# 检验年度
t <- as.character(index(data))
D_factor_year <- as.factor(substr(t,nchar(t)-9,nchar(t)-6))
print(D_factor_year)
D_year <- data.frame(year=D_factor_year,ret=data[,1])
colnames(D_year) <- c("year","ret")
p <- ggplot(D_year,aes(year,ret,col=D_factor_year))+
geom_boxplot()+
labs(x="Year",y="Return (%)",title="Return of every year")
print(p)
}
其中:class(data)为"xts" "zoo"
该函数运行的时候会报错:
Error in eval(expr, envir, enclos) : object 'D_factor_year' not found
这是为什么啊。