Is there an R function that helps with getting monthly return data into yearly means? This is the code I've used so far for returns of the apple stock. It comes in three columns called PERMNO
, Names Date
, and returns
.
This file is originally from an excel spreadsheet, and my job is to try to make yearly means from monthly values in the column returns
.
library(readxl)
#download the monthly stock returns for apple
Apple_StockReturn_M <- read_excel("C:/Users/Didrik/Desktop/2a) apple).xlsx")
#Deleting column 1 called "PERMNO"
Apple_StockReturn_M$PERMNO <- NULL
#Calculating log returns
LogReturn_Apple_Stock = log(1+Apple_StockReturn_M$Returns)
View(LogReturn_Apple_Stock)
#Calculating mean p.a.
Sample data (edit):
df <- structure(list(ID = 1:7, Date = c(19810130L, 19810227L, 19810331L,
19810430L, 19810529L, 19810630L, 19810731L), Return = c(-0.170018,
-0.061674, -0.075117, 0.15736, 0.164474, -0.214689, -0.038369
)), class = "data.frame", row.names = c(NA, -7L))