this will help The second part needs to be done with outer. You can use a^b in a vectorized way, but it will not apply the operation to the first element of a vs all elements of b, the the second element of a vs all elements of b, etc., like you want in this case. It will just give c(a[1]^b[1], a[2]^b[2], ..., a[n]^b[n]).

```
out_vec2 <- rowSums(df[5:100] * outer(1 + abs(df$X1)/100, -(0:95)/12, `^`))
all.equal(out_vec, out_vec2)
# [1] TRUE
```

```
loop <- function(){
out_vec <- c()
for (i in 1:nrow(df)) {
out_vec[i]<- sum(df[i, 5:100] * (1 + abs(df$X1[i])/100) ^ -(0:95 / 12))
}
out_vec
}
vect <- function() rowSums(df[5:100] * outer(1 + abs(df$X1)/100, -(0:95)/12, `^`))
library(microbenchmark)
microbenchmark(loop(), vect(), times = 10)
# Unit: milliseconds
# expr min lq mean median uq max neval
# loop() 12065.34780 12756.12062 13095.97435 12892.87818 13460.56978 15030.0197 10
# vect() 35.73011 41.36212 60.57327 54.40029 79.25182 104.0453 10
```