Maybe one approach could be:

set.seed(123)
df1 <- data.frame(measure = rep(c("A1", "A2", "A3"), each=3),
                  water = sample(c(100:200), 9),
          tide  = sample(c(-10:+10), 9))


100 * tail(df1$tide, -3) / head(df1$water, -3)


I hope it helps.

Best,
Dimitris


On 10/26/2011 12:02 PM, Patrick Hausmann wrote:
Dear R users,

It may be very simple but it is being difficult for me.
I'd like to calculate the difference in percent between to measures.
My data looks like this:

set.seed(123)
df1 <- data.frame(measure = rep(c("A1", "A2", "A3"), each=3),
water = sample(c(100:200), 9),
tide = sample(c(-10:+10), 9))
df1

# What I want to calculate is:
# tide_[A2] / water_[A1],
# tide_[A3] / water_[A2]

# This 'works' for the example, but I am
# looking for a more general solution.

df1$tide_diff <- ave(df1$tide, FUN=function(L) L /
c(NA, NA, NA, df1$water)) * 100
df1

Thanks for any help!
Patrick

______________________________________________
R-help@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.


--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/

______________________________________________
R-help@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.

Reply via email to