Question: I have the following input data frame:

```
df1 <- data.frame(row.names=c("AA","BB","CC","DD","EE"), A=c(0.90,0.75,0.65,0.55,0.45), B=c(0.80,0.75,0.60,0.60,0.45))

Row  A   B
AA   0.90 0.80
BB   0.75 0.75
CC   0.65 0.60
DD   0.55 0.60
EE   0.45 0.45

```

Is there a way to divide the values in row AA by the value in row BB (0.90/0.75 = 1.200). Then, I wish to divide this new value in row BB by the value in row CC (1.2/0.65 = 1.846) etc. to produce the following output data frame:

```
Row  A     B
A   0.900 1.067
B   1.200 1.067
C   1.846 1.778 
D   3.357 2.963
E   7.459 6.584

```

I'm really scratching my head with this one so any help is much appreciated. 

Thanks guys!

Answer: 
You could use a simple `for` loop or `cumprod`:

```
df2 <- df1
for (i in 2:nrow(df2)) {
  df2[i, ] <- df2[i-1,]/df2[i,]
}

```

Depending on your number of columns the `cumprod` approach should be a lot of faster:

```
df3 <- df1
df3$A <- cumprod(c(df3[1, "A"], 1/df3[-1, "A"]))
df3$B <- cumprod(c(df3[1, "B"], 1/df3[-1, "B"]))

all.equal(df2, df3)
# TRUE

```
I would use `Reduce` on each column, with `accumulate = TRUE`

```
as.data.frame(lapply(df1, Reduce, f = `/`, accumulate = TRUE))

```

(row names are lost in the process, but I bet you'll know what to do to get them back.)