# r scripts

# names?
mtcars |> names()

# important: group_by does not change how the data looks
library(tidyverse)
library(nycflights13)
flights |>
  group_by(year, month, day) |>
  summarize(mx = max(arr_delay, na.rm = TRUE)) |>
  arrange((-mx)) |>
  print(n = 30)

flights |> group_by(month) |> summarize(avg_dep_time = mean(dep_time))

flights |>
  group_by(carrier) |>
  summarize(avg_dep_time = mean(dep_time, na.rm = TRUE))


diamonds |> head()


flights |>
  group_by(year, month, day) |>
  summarize(mx = max(arr_delay, na.rm = TRUE)) |>
  arrange((-mx)) |>
  print(n = 30) |>
  summarize(m = mean(mx)) |>
  arrange(-m)

penguins |> summarize(mx_bd = max(bill_depth_mm, na.rm = TRUE))

penguins |>
  group_by(species) |>
  summarize(mx_bd = max(bill_depth_mm, na.rm = TRUE))

penguins |>
  group_by(species) |>
  summarize(
    mx_bd = max(bill_depth_mm, na.rm = TRUE),
    howmanywegot = n()
  )


diamonds |>
  group_by(carat) |>
  summarize(
    mean_price = mean(price),
  )

q <- quantile(diamonds$carat)

# when you wish to make a categorical variable
# out of a numeric one, use cut()

dc <- cut(diamonds$carat, breaks = q, labels = c('sm', 'med', 'lg', 'xl'))

dim(dc)


# grouped mean
# compute the grouped-mean of all numeric variables
# computes a mean of all numeric variables, but do so
# by "cut"
diamonds |>
  group_by(cut) |>
  summarise(across(
    where(is.numeric),
    mean
  ))

diamonds |> group_by(cut) |> summarize(carat_mean = mean(depth))

# look at the diamond data of the most expensive diamond
diamonds |> slice_max(price, n = 5)

# the largest?
diamonds |> slice_max(carat)

# find the most expensive "fair" diamond
# costs 18574
# think: among only the 'fair' diamonds, arrange by price
# this shows the most expensive by cut
group_by(diamonds, cut) |> slice_max(price)

# how does the most expensive fair diamond rank on the
# list of expensive diamonds
# what rank is the most expensive fair diamond
a <- arrange(diamonds, desc(price)) |>
  mutate(ranking = row_number(), .before = 1)

b <- arrange(diamonds, desc(price), cut) |>
  mutate(ranking = row_number(), .before = 1)

arrange(diamonds, desc(price)) |>
  mutate(ranking = row_number(), .before = 1) |>
  filter(cut == 'Fair')

Titanic

d <- as.data.frame(Titanic)
glimpse(d)

a <- group_by(d, Class, Sex) |>
  summarize(n = sum(Freq))


#install.packages("ggstats")
library(ggstats)
library(tidyverse)

p <- ggplot(d) +
  # what happens when you remove "weight = Freq"?
  aes(x = Class, fill = Survived, weight = Freq, by = Class) +
  geom_bar(position = "fill") +
  # halfway up the stack
  geom_text(stat = "prop", position = position_stack(.5)) +
  labs(title = "Both Counts & Percentages", y = "Total")

print(p)


p <- ggplot(d) +
  aes(x = Class, fill = Survived, weight = Freq, by = Class) +
  geom_bar(position = "fill") +
  geom_text(stat = "prop", position = position_fill(0.5)) +
  labs(title = "Proportions & Percentages", y = "Percent") +
  facet_grid(~Sex)

print(p)


(Note: Always include the survey weight PERWT in your extract so students can practice weighting summaries or use srvyr.)
