##
## 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
Read in the Chicago 2016 bike share data.
# Read in data
bikeShare <- read.csv("Chicago-2016-Summary.csv", header = TRUE)
# Look at data frame structure to see variable types
str(bikeShare)
## 'data.frame': 72131 obs. of 5 variables:
## $ duration : num 15.43 3.3 2.07 19.68 10.93 ...
## $ month : int 3 3 3 3 3 3 3 3 3 3 ...
## $ hour : int 23 22 22 22 22 21 21 20 20 20 ...
## $ day_of_week: chr "Thursday" "Thursday" "Thursday" "Thursday" ...
## $ user_type : chr "Subscriber" "Subscriber" "Subscriber" "Subscriber" ...
Turn the month, hour, day of week, and user type columns into factors.
# Count all unique data entries per column to understand how to set levels
bikeShare |> count(month)
## month n
## 1 1 1901
## 2 2 2394
## 3 3 3719
## 4 4 4567
## 5 5 7211
## 6 6 9794
## 7 7 10286
## 8 8 9810
## 9 9 8700
## 10 10 7160
## 11 11 4811
## 12 12 1778
bikeShare |> count(hour)
## hour n
## 1 0 482
## 2 1 326
## 3 2 175
## 4 3 88
## 5 4 137
## 6 5 538
## 7 6 2024
## 8 7 4256
## 9 8 5454
## 10 9 3190
## 11 10 2820
## 12 11 3693
## 13 12 4208
## 14 13 4313
## 15 14 4199
## 16 15 4739
## 17 16 6622
## 18 17 8564
## 19 18 5786
## 20 19 3905
## 21 20 2519
## 22 21 1925
## 23 22 1346
## 24 23 822
bikeShare |> count(day_of_week)
## day_of_week n
## 1 Friday 10741
## 2 Monday 11286
## 3 Saturday 9927
## 4 Sunday 9654
## 5 Thursday 10008
## 6 Tuesday 10911
## 7 Wednesday 9604
bikeShare |> count(user_type)
## user_type n
## 1 Customer 17149
## 2 Subscriber 54982
# Create levels for each column (variable)
## Month
### Initiate conversion from integers to abbreviations
bs_month <- c(
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
)
## Hour
### Initiate conversion from integers to hr:min
bs_hour <- 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"
)
## Day of Week
bs_dayWeek <- c(
"Friday", "Monday", "Saturday", "Sunday", "Thursday", "Tuesday", "Wednesday"
)
## User Type
bs_userType <- c(
"Customer", "Subscriber"
)
# Convert each column into a factor
bikeShare_factors <- bikeShare %>%
mutate(
month = factor(month.abb[month], levels = month.abb, ordered = TRUE), ## Convert integers to abbvs.
hour = factor(sprintf("%02d:00", hour), levels = bs_hour), ## Convert integers to hr:min
day_of_week = factor(day_of_week, levels = bs_dayWeek),
user_type = factor(user_type, levels = bs_userType)
)
# Ensure that levels were made appropriately
levels(bikeShare_factors$month)
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
levels(bikeShare_factors$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(bikeShare_factors$day_of_week)
## [1] "Friday" "Monday" "Saturday" "Sunday" "Thursday" "Tuesday"
## [7] "Wednesday"
levels(bikeShare_factors$user_type)
## [1] "Customer" "Subscriber"
# Ensure that the specified columns were indeed changed to factors
## Also double check to see if format changes to month and hour were successful
str(bikeShare_factors)
## 'data.frame': 72131 obs. of 5 variables:
## $ duration : num 15.43 3.3 2.07 19.68 10.93 ...
## $ month : Ord.factor w/ 12 levels "Jan"<"Feb"<"Mar"<..: 3 3 3 3 3 3 3 3 3 3 ...
## $ hour : Factor w/ 24 levels "00:00","01:00",..: 24 23 23 23 23 22 22 21 21 21 ...
## $ day_of_week: Factor w/ 7 levels "Friday","Monday",..: 5 5 5 5 5 5 5 5 5 5 ...
## $ user_type : Factor w/ 2 levels "Customer","Subscriber": 2 2 2 2 2 2 2 2 2 2 ...