2

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))
Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
  • 1
    Can you give us a sample of your data. I don't know how many rows are in the data, but if you use `dput(Apple_StockReturn_M)` or `dput(head(Apple_StockReturn_M, n = 20))`, you can give us some data to copy into our R sessions and experiment with. Also, what is the formula for calculating yearly means from monthly returns? Is it the simple arithmetic mean, the geometric mean (useful if some returns are negative), or some other formula? – Ben Norris Sep 27 '20 at 20:30
  • What is the problem? Did you try `mean`? – Parfait Sep 27 '20 at 20:30
  • @BenNorris hey! thanks for helping. My data contains 468 rows and two columns. Im trying to use the arithmic mean, but, i havnt got a formula to play with. Therefore I have to gather 12 months of data, aka, 12 rows of data and calculate the mean for them all. I'll give you some data shortly. – Didrik Moy Sund Sep 27 '20 at 20:58
  • @BenNorris 1 19810130 -0.170018 2 19810227 -0.061674 3 19810331 -0.075117 4 19810430 0.157360 5 19810529 0.164474 6 19810630 -0.214689 7 19810731 -0.038369 the first number indicates the row for example "19810731" is simply 1981-07-31 and for example -0,038369 is the return (data) im playing with. Im sorry that i couldnt post the data more elegant. This comment function wouldnt let me – Didrik Moy Sund Sep 27 '20 at 21:05
  • @BenNorris 468 columns, two rows. im sorry for the mix up – Didrik Moy Sund Sep 27 '20 at 21:12
  • @DidrikMoySund - Try using `dput(head(Apple_StockReturn_M[,1:24]))` to give the first 24 columns and pasting the output into your question. The output of dput can be used to create an input that anyone can copy into R to create your object. – Ben Norris Sep 27 '20 at 21:13
  • @BenNorris I tried the dput function and it worked pretty good. the function "dput(head(Apple_StockReturn_M[,1:24]))" pasted the first 24 numbers. Do you know how I can paste from 25-(for example) 50? – Didrik Moy Sund Sep 27 '20 at 21:56
  • (1) `x[,1:24]` gives columns 1 through 24, so `x[,25:50]` gives columns 25 through 50. But I think you're missing the point ... (2) I think @BenNorris's point is that questions on SO do well when they are *small* (not too much data, but enough to work) and *reproducible* (you've included enough code so that we can reproduce your problem in order to suggest solutions). So Ben's point was that you give us a small sample of your data, in a way that is unambiguous and reasily used. See https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Sep 27 '20 at 22:44

0 Answers0