Challenge9_MarcelaRobinson

challenge9
writing_functions
descriptive_statistics
Creating a function
Author

Marcela Robinson

Published

January 25, 2023

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0     ✔ purrr   1.0.1
✔ tibble  3.1.8     ✔ dplyr   1.1.0
✔ tidyr   1.3.0     ✔ stringr 1.5.0
✔ readr   2.1.3     ✔ forcats 1.0.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(here)
here() starts at /home/runner/work/601_Winter_2022-2023/601_Winter_2022-2023

Create a function

#Create a function to obtain descriptive statistics
descrip_statistics<-function(x){
  st_mean <- mean(x, na.rm = TRUE)
  st_median <- median(x, na.rm = TRUE)
  st_sd <- sd(x, na.rm = TRUE)
  st_min <- min(x, na.rm = TRUE)
  st_max <- max(x, na.rm = TRUE)
  st_range <- range(x, na.rm = TRUE)
  st_var <- var(x, na.rm = TRUE)
  return(list(mean=st_mean, median=st_median, sd = st_sd, min = st_min, max = st_max, range = st_range, var = st_var))
}

Test my function

#Read in a dataset
testdf<- here("posts", "_data", "AB_NYC_2019.csv")%>%
  read_csv
Rows: 48895 Columns: 16
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr   (5): name, host_name, neighbourhood_group, neighbourhood, room_type
dbl  (10): id, host_id, latitude, longitude, price, minimum_nights, number_o...
date  (1): last_review

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#test my function
descrip_statistics(testdf$price)
$mean
[1] 152.7207

$median
[1] 106

$sd
[1] 240.1542

$min
[1] 0

$max
[1] 10000

$range
[1]     0 10000

$var
[1] 57674.03