title: Calculating yearly log return from daily closing price in R for a panel of companies
body: <p>I have a panel data set of 34 banks from 2007 to 2020. I need to calculate the yearly log return from the daily adjusted closing stock prices of these banks. The data structure is as follows:</p>
  <p><a href="https://i.stack.imgur.com/qaNVZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qaNVZ.png" alt="enter image description here" /></a></p>
  <p>The date after importing to R is in POSIXct format while prices are in number format. There are in total 121323 observations.</p>
  <p>Can anyone help me with the codes to calculate annual returns from daily prices? One of the problems is accounting for the panel data structure, the return calculation for Bank A has to end on the last date and the process has to repeat for Bank B and end on the last date so on. In the above data structure, the return calculation has to end on 07-01-2007 for Bank A. I have tried using quantmod but was unsuccessful. Any help is much appreciated.</p>
  <p>Thanks</p>