From 2cd2080e14b5a964f93a7ec5ecd33ede665b5fcc Mon Sep 17 00:00:00 2001 From: Ethan White Date: Fri, 28 Oct 2022 09:39:24 -0400 Subject: [PATCH] Drop unneeded complexity in vectorization --- materials/iteration-without-loops-R.md | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/materials/iteration-without-loops-R.md b/materials/iteration-without-loops-R.md index dce6c24e8..ea363545e 100644 --- a/materials/iteration-without-loops-R.md +++ b/materials/iteration-without-loops-R.md @@ -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 @@ -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) ```