[R] fraction with timelag

Bill.Venables at csiro.au Bill.Venables at csiro.au
Thu Mar 24 12:59:26 CET 2011


 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.



More information about the R-help mailing list