Re: [R] fraction with timelag

From: <Bill.Venables_at_csiro.au>
Date: Thu, 24 Mar 2011 22:59:26 +1100

 How about simply

> df <- data.frame(id = 1:6,

+                   xout = c(12.34, 21.34, 2.34, 4.56, 3.24, 3.45),
+                   xin = c(NA, 34,67,87,34, NA))

>
> with(df, c(NA, xin[-1]/xout[-length(xout)]))
[1] NA 2.755267 3.139644 37.179487 7.456140 NA
>

BTW You seem to have some decimal points missing in your example.

BTW2: zoo?



From: r-help-bounces_at_r-project.org [r-help-bounces_at_r-project.org] On Behalf Of Patrick Hausmann [patrick.hausmann_at_uni-bremen.de] Sent: 24 March 2011 21:31
To: r-help_at_r-project.org
Subject: [R] fraction with timelag

Dear r-help,

I'm having this DF

df <- data.frame(id = 1:6,

                  xout = c(1234, 2134, 234, 456, 324, 345),
                  xin= c(NA, 34,67,87,34, NA))

and would like to calculate the fraction (xin_t / xout_t-1) The result should be:
# NA, 2.76, 3.14, 37.18, 7.46, NA

I am sure there is a solution using zoo... but I don't know how...

Thanks for any help!
Patrick



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.

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. Received on Thu 24 Mar 2011 - 12:05:13 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Mar 2011 - 12:10:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive