diff --git a/modules/Functions/Functions.html b/modules/Functions/Functions.html index ce2ac076..31ea37b6 100644 --- a/modules/Functions/Functions.html +++ b/modules/Functions/Functions.html @@ -3117,7 +3117,7 @@ div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} ul.task-list{list-style: none;} pre > code.sourceCode { white-space: pre; position: relative; } -pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } +pre > code.sourceCode > span { line-height: 1.25; } pre > code.sourceCode > span:empty { height: 1.2em; } .sourceCode { overflow: visible; } code.sourceCode > span { color: inherit; text-decoration: inherit; } @@ -3473,45 +3473,10 @@

Functions for tibbles

-

select(n) will choose column n:

- -
get_index <- function(dat, row, col) {
-  dat %>%
-    filter(row_number() == row) %>%
-    select(all_of(col))
-}
-
-get_index(dat = ces, row = 10, col = 7)
- -
# A tibble: 1 × 1
-  CES4.0Score
-        <dbl>
-1        43.7
- -

Functions for tibbles

- -

Including default values for arguments:

- -
get_top <- function(dat, row = 1, col = 1) {
-  dat %>%
-    filter(row_number() == row) %>%
-    select(all_of(col))
-}
-
-get_top(dat = ces)
- -
# A tibble: 1 × 1
-  CensusTract
-        <dbl>
-1  6001400100
- -

Functions for tibbles

-

Can create function with an argument that allows inputting a column name for select or other dplyr operation:

clean_dataset <- function(dataset, col_name) {
   my_data_out <- dataset %>% select({{col_name}}) # Note the curly braces
-  write_csv(my_data_out, "clean_data.csv")
   return(my_data_out)
 }
 
@@ -3532,6 +3497,27 @@ 

10 43.7 # ℹ 8,025 more rows
+
get_mean <- function(dat, county_name, col_name) {
+  my_data_out <- dat %>% 
+    filter(str_detect(CaliforniaCounty, county_name)) %>%  
+    summarise(mean = mean({{col_name}}, na.rm = TRUE))
+    return(my_data_out)
+}
+
+get_mean(dat = ces, county_name = "Alameda", col_name = CES4.0Score)
+ +
# A tibble: 1 × 1
+   mean
+  <dbl>
+1  22.9
+ +
get_mean(dat = ces, county_name = "Fresno", col_name = CES4.0Score)
+ +
# A tibble: 1 × 1
+   mean
+  <dbl>
+1  40.9
+

Summary