[R] diff in a dataframe

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jan 10 18:10:03 CET 2008


Represent this as a time series.  Using
the zoo package:

> library(zoo)
> z <- zoo(cbind(price_g = c(0.34, 0.36), price_s = c(0.56, 0.76)), as.Date(c("2000-01-01", "2000-01-05")))
> diff(log(z))
              price_g   price_s
2000-01-05 0.05715841 0.3053816
> diff(log(z), na.pad = TRUE)
              price_g   price_s
2000-01-01         NA        NA
2000-01-05 0.05715841 0.3053816


See the two zoo vignettes:
vignette("zoo")
vignette("zoo-quickref")

On Jan 10, 2008 2:16 AM, Vishal Belsare <shoot.spam at gmail.com> wrote:
> I have a dataframe say:
>
> date   price_g   price_s
>         0.34        0.56
>         0.36        0.76
>           .              .
>           .              .
>           .              .
>
> and so on. say, 1000 rows.
>
> Is it possible to add two columns to this dataframe, by computing say
> diff(log(price_g) and diff(log(price_s)) ?
>
> The elements in the first row of these columns cannot be computed, but
> can I coerce this to happen and assign a missing value there? It would
> be really great if I could do that, because in this case I don't have
> to re-index my transformed series to the dates again in a new
> dataframe.
>
> Thanks in anticipation.
>
>
> Vishal Belsare
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list