Skip to content

Commit

Permalink
Drop unneeded complexity in vectorization
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanwhite committed Oct 28, 2022
1 parent ca9f1cc commit 2cd2080
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions materials/iteration-without-loops-R.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,12 @@ lapply(volumes, est_mass_max)
* But our coefficient was calculated with log-transformed data so we need to convert it in our function

```r
est_mass_coef <- function(volume, coefficient){
a <- exp(coefficient)
est_mass_coef <- function(volume, a){
mass <- a * volume ^ 0.9
return(mass)
}
```

* `exp()` is vectorized

```r
exp(c(1, 2, 3))
```

* So our whole function should still be vectorized

```r
est_mass_coef(volumes, 0.97)
est_mass_coef(volumes, 2.56)
```

* Because we only provided a single value of `coefficient`, that value gets used for every value of `volume` when doing the calculation
Expand All @@ -179,7 +168,7 @@ c(1, 2, 3) * c(1, 2, 3)
* So we can also pass the function a vector of coefficients

```r
coefs <- c(0.97, 0.5, 2.2)
coefs <- c(2.56, 1, 3.2)
est_mass_coef(volumes, coefs)
```

Expand Down

0 comments on commit 2cd2080

Please sign in to comment.