Kou - 1 month ago 15

R Question

Suppose I have a data frame like this

`> dat`

a b c

1 1 0.3321008 0.3321008

2 2 -0.2946729 NA

3 3 -0.1447266 NA

4 4 -0.9415429 NA

5 5 -1.0165080 NA

here is the

`dput`

`structure(list(a = 1:5, b = c(0.332100835317822, -0.294672931641969,`

-0.144726592564241, -0.941542877670977, -1.0165079846083), c = c(0.332100835317822,

NA, NA, NA, NA)), .Names = c("a", "b", "c"), row.names = c(NA,

-5L), class = "data.frame")

I would like to perform an operation on the

`c`

`c = lag(c)*b`

I can do this using a simple for loop as below

`for(i in (1:4)){`

dat$c[i+1] <- dat$c[i]*dat$b[i+1]

}

Output:

`> dat`

a b c

1 1 0.3321008 0.33210084

2 2 -0.2946729 -0.09786113

3 3 -0.1447266 0.01416311

4 4 -0.9415429 -0.01333517

5 5 -1.0165080 0.01355531

How do I do this using dplyr mutate ? or using apply functions?

Answer

By doing the math, we can avoid the iterative computation:

```
library(dplyr)
dat %>% mutate(c = cumprod(replace(b, 1, 1))*c[1])
```

Source (Stackoverflow)

Comments