'How to apply an operation to a specific Column in R [duplicate]

I have date in the first column and prices of a stock in the second column and I have imported this file in R as Y. I want the log differences of the closing prices while preserving the first column of date as is. I want to save the result as spReturns.

I came up with the following code:

spReturns = df <- transform(Y, Close = diff(log(Close)))

But, I am getting this error:

Error in `[<-.data.frame`(`*tmp*`, inx[matched], value = list(Close = c(0.0100854932368533,  : 
  replacement element 1 has 1826 rows, need 1827
r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source