[R] Log difference in a dataframe column

Peter Ehlers ehlers at ucalgary.ca
Mon Jan 17 20:23:55 CET 2011


On 2011-01-17 07:44, eric wrote:
>
> What am I doing wrong here ? And what's the right way to calculate the log
> differences in a column in a df ?
>
> # first 3 rows of 5000 rows
> y[1:3,]
>
>   Date  Open  High   Low Close
> 1 1983-03-30 29.96 30.51 29.96 30.35
> 2 1983-03-31 30.35 30.55 30.20 30.24
> 3 1983-04-04 30.25 30.65 30.24 30.39
>
> #equation in question ...why is this giving zeros ?
> y1<- 100*log(y[,5]/(lag(y[,5],1)))
>
> # first 10 values from the equation...all zeros
> head(y1,10)
>   [1] 0 0 0 0 0 0 0 0 0 0

Well, take a look at the output of lag().
Try it with as.ts(y[, 5]) replacing y[, 5].

Peter Ehlers



More information about the R-help mailing list