Load in the necessary packages
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readr)
## Warning: package 'readr' was built under R version 4.4.3
Read in the CSV data
bikes <- read.csv("NYC-2016-Summary.csv")
head(bikes)
## duration month hour day_of_week user_type
## 1 13.98333 1 0 Friday Customer
## 2 11.43333 1 0 Friday Subscriber
## 3 5.25000 1 0 Friday Subscriber
## 4 12.31667 1 0 Friday Subscriber
## 5 20.88333 1 0 Friday Customer
## 6 8.75000 1 0 Friday Subscriber
Make a month value and rename to 3 letter names
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
Convert the numerical months into a factor with the 3 letter
names
bikes <- bikes %>%
mutate(month = factor(months[month],
levels = months,
ordered = TRUE))
Making values of hours
hours <- c("00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00")
Making columns as levels
user <- c("Subscriber", "Customer")
bikes <- bikes %>%
mutate(user_type = factor(user_type,
levels = user))
days <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
bikes <- bikes %>%
mutate(day_of_week = factor(day_of_week, levels = days, ordered = TRUE))
Checking there is no duplicates or typos in valid levels
levels(bikes$hour)
## [1] "00:00" "01:00" "02:00" "03:00" "04:00" "05:00" "06:00" "07:00" "08:00"
## [10] "09:00" "10:00" "11:00" "12:00" "13:00" "14:00" "15:00" "16:00" "17:00"
## [19] "18:00" "19:00" "20:00" "21:00" "22:00" "23:00"
levels(bikes$month)
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
levels(bikes$day_of_week)
## [1] "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
## [7] "Sunday"
levels(bikes$user_type)
## [1] "Subscriber" "Customer"
str(bikes)
## 'data.frame': 276798 obs. of 5 variables:
## $ duration : num 13.98 11.43 5.25 12.32 20.88 ...
## $ month : Ord.factor w/ 12 levels "Jan"<"Feb"<"Mar"<..: 1 1 1 1 1 1 1 1 1 1 ...
## $ hour : Ord.factor w/ 24 levels "00:00"<"01:00"<..: 1 1 1 1 1 1 1 2 2 2 ...
## $ day_of_week: Ord.factor w/ 7 levels "Monday"<"Tuesday"<..: 5 5 5 5 5 5 5 5 5 5 ...
## $ user_type : Factor w/ 2 levels "Subscriber","Customer": 2 1 1 1 2 1 1 1 1 2 ...