Objectives for this lab: - Last four columns all factors - Months renamed to 3 letter names(Jan, Dec) but correctly ordered - Hour reformatted as time of day - levels() function echoed to show that ordering is correct and no duplicates/typos in the valid levels
library("tidyverse")
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library("dplyr")
library("lubridate")
library("chron")
##
## Attaching package: 'chron'
##
## The following objects are masked from 'package:lubridate':
##
## days, hours, minutes, seconds, years
reading in NYC summary csv file and naming it “NYC”
setwd("~/Desktop/BIN501/W4")
NYC<-read.csv("NYC-2016-Summary.csv", header = TRUE)
renaming months from numeric values (1-12) to 3 letter abbreviations
NYC$month <- month.abb[as.numeric(NYC$month)]
changing hours into time of day from 1-23 to 00:00 format
NYC$hour <- format(strptime(NYC$hour, format = "%H"), format="%H:%M")
taking NYC data and changing last 4 columns to factors via lapply function
NYC[2:5]<-lapply(NYC[2:5], factor)
Making sure data is ordered by month and day of the week
NYC$month=factor(NYC$month, levels = month.abb)
NYC$day_of_week=factor(NYC$day_of_week, levels = c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'))
Using levels function to show order is correct and there are no duplicates/typos in the valid levels
levels(NYC$month)
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
levels(NYC$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(NYC$day_of_week)
## [1] "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
## [7] "Sunday"
levels(NYC$user_type)
## [1] "" "Customer" "Subscriber"
making a violin plot to look at bike share data for durations of use each month
ggplot(NYC, aes(x= month, y= duration, ordered=TRUE)) +
geom_violin()