##installs and runs tidyverse to be used in analysis ##runs readr in order to upload data

install.packages ("tidyverse")
library ("tidyverse")
install.packages("readr")
library(readr)
install.packages(ggplot2)
library(ggplot2)

##install and load for data cleaning

install.packages("here")
install.packages("skimr")
install.packages("janitor")
install.packages("dplyr")
install.packages("lubridate")
library("here")
library("skimr")
library("janitor")
library("dplyr")
library("lubridate")

##set working directory in order to get data imported

setwd("C:/Users/bensc/Desktop/Bike_Sharing")

##uploads 12 months of bike_share data

X202307_divvy_tripdata <- read_csv("202307-divvy-tripdata.csv") ## month 1
X202308_divvy_tripdata <- read_csv("202308-divvy-tripdata.csv") ## month 2
X202309_divvy_tripdata <- read_csv("202309-divvy-tripdata.csv") ## month 3
X202310_divvy_tripdata <- read_csv("202310-divvy-tripdata.csv") ## month 4
X202311_divvy_tripdata <- read_csv("202311-divvy-tripdata.csv") ## month 5
X202312_divvy_tripdata <- read_csv("202312-divvy-tripdata.csv") ## month 6
X202401_divvy_tripdata <- read_csv("202401-divvy-tripdata.csv") ## month 7
X202402_divvy_tripdata <- read_csv("202402-divvy-tripdata.csv") ## month 8
X202403_divvy_tripdata <- read_csv("202403-divvy-tripdata.csv") ## month 9
X202404_divvy_tripdata <- read_csv("202404-divvy-tripdata.csv") ## month 10
X202405_divvy_tripdata <- read_csv("202405-divvy-tripdata.csv") ## month 11
X202406_divvy_tripdata <- read_csv("202406-divvy-tripdata.csv") ## month 12

##Shows view of data

View(X202307_divvy_tripdata) ## 1st Month dataset
View(X202308_divvy_tripdata) ## 2nd month dataset
View(X202309_divvy_tripdata) ## 3rd month dataset
View(X202310_divvy_tripdata) ## 4th month dataset
View(X202311_divvy_tripdata) ## 5th month dataset
View(X202312_divvy_tripdata) ## 6th month dataset
View(X202401_divvy_tripdata) ## 7th month dataset
View(X202402_divvy_tripdata) ## 8th month dataset
View(X202403_divvy_tripdata) ## 9th month dataset
View(X202404_divvy_tripdata) ## 10th month dataset
View(X202405_divvy_tripdata) ## 11th month dataset
View(X202406_divvy_tripdata) ## 12th month dataset

##One of the final steps of analysis – ##trying to combine all 12 months a data now that I have my code ready ##I used one month to write code and prepare cleaning and analysis

year_bike_data <- bind_rows(X202307_divvy_tripdata, X202308_divvy_tripdata, X202309_divvy_tripdata, 
                            X202310_divvy_tripdata, X202311_divvy_tripdata, X202312_divvy_tripdata,
                            X202401_divvy_tripdata, X202402_divvy_tripdata, X202403_divvy_tripdata,
                            X202404_divvy_tripdata, X202405_divvy_tripdata, X202406_divvy_tripdata)

##cleaning data and figuring out total ride times

ride_time_1_1 <- year_bike_data %>%
  mutate(diff = ended_at - started_at)

##deleting data with any missing cells

clean_ride_time_1_2 <- na.omit(ride_time_1_1) %>%
  arrange(diff)

##truncating lat and long data to the nearest tenth ##assuming some riders returned bikes at the end of the rack, which would cause a different location data point

clean_ride_time_1_3 <- clean_ride_time_1_2 %>%
  mutate(start_lat = round(start_lat, 1)) %>%
  mutate(start_lng = round(start_lng, 1)) %>%
  mutate(end_lat = round(end_lat, 1)) %>%
  mutate(end_lng = round(end_lng, 1))

##using lat and long data to make a new column to address the issues with station names and id’s

clean_ride_time_1_4 <- clean_ride_time_1_3 %>%
  mutate(same_different = if_else(
    (start_lat == end_lat & start_lng == end_lng) | start_station_name == end_station_name | start_station_id == end_station_id,
    "same",
    "different"))

##deleting any rides that are less than 3 minutes AND start and end at the same station ##assuming this would be an error in rental and returned right away

clean_ride_time_1_5 <- clean_ride_time_1_4 %>%
  filter(!(same_different == "same" & diff < 180),) %>%
  arrange(diff)

##two pieces of data need to be removed ##rides were 0 seconds to a different station and 5 second to a different station ##I’m leaving all rides that are over 30 seconds long to different stations

clean_ride_time_1_6 <- clean_ride_time_1_5 %>%
  filter(!(same_different == "different" & diff < 30),) %>%
  arrange(diff)

##creates a new column with day of the week for when each ride starts

clean_ride_time_1_7 <- clean_ride_time_1_6 %>%
  mutate(day_of_week = wday(started_at, label = TRUE, abbr = FALSE))

##creates a new column with the hour of the day when each ride starts

clean_ride_time_1_8 <- clean_ride_time_1_7 %>%
  mutate(hour_of_day = hour(started_at))

##sort into just members

members <- clean_ride_time_1_8 %>%
  filter(!(member_casual == "member"))

##sort into just casuals

casuals <- clean_ride_time_1_8 %>%
  filter(!(member_casual == "casual"))

##find the mean of ride times for the total dataset

mean_value_1_8 <- mean(clean_ride_time_1_8$diff)
print(mean_value_1_8)

##find the mean of ride times for members

mean_value_members <- mean(members$diff)
print(mean_value_members)

##find the mean of ride times for casuals

mean_value_casuals <- mean(casuals$diff)
print(mean_value_casuals)

##calculates and prints the amount of rides that happen during each hour of the day

counts <- clean_ride_time_1_8 %>%
  count(hour_of_day)
    print(counts, n = 24)

##calculates and prints the amount of rides that happen during each hour for members

counts <- clean_ride_time_1_8 %>%
  filter(member_casual == "member") %>%
  count(hour_of_day)
  print(counts, n = 24)

##These are my run codes to check data throughout the cleaning and analyzing process

skim_without_charts(clean_ride_time_1_7)
View(clean_ride_time_1_7)
skim_without_charts(clean_ride_time_1_8)
View(clean_ride_time_1_8)

skim_without_charts(same_station)
skim_without_charts (members)
View(members)
skim_without_charts (casuals)
View(casuals)

counts <- members %>%
  count(day_of_week)

counts <- casuals %>%
  count(day_of_week)

counts <- clean_ride_time_1_8 %>%
  count(same_different)

counts <- clean_ride_time_1_8 %>%
  count(day_of_week)