From e0de0033e8aeb17053caea38c25d39698e59e4e2 Mon Sep 17 00:00:00 2001 From: Ethan White Date: Tue, 8 Oct 2024 12:16:18 -0400 Subject: [PATCH] Add comparison of named vs passed cols for embracing This distinction is important and used in our exercise --- materials/functions-R.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/materials/functions-R.md b/materials/functions-R.md index 1d41d158..02409570 100644 --- a/materials/functions-R.md +++ b/materials/functions-R.md @@ -241,6 +241,24 @@ make_plot(surveys, hindfoot_length, "Hindfoot Length [mm]") make_plot(surveys, weight, "Weight [g]") ``` +* Only need to embrace column names that we are passing as arguments +* If we assume there is a column with some name in the data frame it doesn't need embracing + +```r +library(tidyr) + +create_time_series <- function(df, column){ + time_series <- df |> + drop_na({{ column }}) |> # column is a variable for a column name, needs {{}} + group_by(year) |> # year is an actual column in the data frame, no {{}} + summarize(avg_size = mean({{ column }})) + return(time_series) +} + +create_time_series(surveys, weight) +create_time_series(surveys, hindfoot_length) +``` + > Do [Writing Tidyverse Functions]({{ site.baseurl }}/exercises/Functions-writing-tidyverse-functions-R). ### Code design with functions