I have the below dataset and try to subset the data into 4 different periods and plot.
library(quantmod)
i<-"AAPL"
i_qt<-getSymbols(i,from="2023-01-01",to="2024-07-01",auto.assign=FALSE)[,6]
plot_4_ranges<-function(data,start_date,end_date,title){
#set the plot window to be 2 rows and 2 columns
par(mfrow=c(2,2))
for (i in 1:4){
#create a string with the appropriate date range
range<-paste(start_date[i],"::",end_date[i],sep="")
#create the price vector and statistics
time_series<-date[range]
mean_data<-round(mean(time_series,na.rm=TRUE),2)
sd_data<-round(sd(time_series,na.rm=TRUE),2)
#plot the histogram along with a legend
hist_title<-paste(title,range)
hist(time_series,breaks=100,prob=T,
xlab="",main=hist_title,cex.main=0.8)
legend("topright",cex=0.7,bty="n",
paste("mean"=mean_data,";sd=",sd_data))
}
#reset the plot window
par(mfrow=c(1,1))
}
#define start and end dates of interest
start_dates<-c("2024-01-02","2024-02-02","2024-03-03","2024-04-04")
end_dates<-c("2024-02-01","2024-03-02","2024-04-03","2024-05-04")
plot_4_ranges(i_qt,begin_dates,end_dates,"Title")
Error in date[range] : object of type 'closure' is not subsettable
The date column should be subsettable, and the defined dates of interest should fall into the data range, any ideas on why is there an error? Many thanks.