-1

I´m trying to insert a column in a dataframe named Reference with returns of stocks obtained from another dataframe called Quotes.

Quotes <- data.frame (Ticker = c("Petr4","Petr4","Petr4","Abev3","Abev3","Petr4","Petr4","Abev3","Abev3","Abev3" ), 
                  Close = c(15.80,15.55,15.90,18.52,19.57,16.25,16.50,19.20,19.18,19.7))

Reference <- aggregate(cbind(Close) ~ Ticker, data = (Quotes), FUN = last)

This is how the inserted column would like (Last N Days return):

Reference$Return <- c(0.044303797,0.063714903)

I´m using Quantmod FUN Last to get the last quote, any other idea?

neilfws
  • 32,751
  • 5
  • 50
  • 63
WsnCode
  • 39
  • 5
  • 1
    Could you be more specific about how is the c(0.044303797,0.063714903) calculated from the given dataframes? – Kim Oct 18 '17 at 22:06

1 Answers1

0

The value on Reference is divided by the lagged value on Quote. In the first case 16.50 ( Last value of Petr4 divided by its lagged (-5)value of 15.8 minus 1.

WsnCode
  • 39
  • 5