library(ggplot2)
library(ggthemes)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(dplyr)
##
## 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(tidyr)
library(tidyverse) # metapackage of all tidyverse packages
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ purrr 1.0.2 ✔ tibble 3.2.1
## ✔ readr 2.1.5
## ── 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(DT)
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
colors = c("#CC1011", "#665555", "#05a399", "#cfcaca", "#f5e840", "#0683c9", "#e075b0")
colors
## [1] "#CC1011" "#665555" "#05a399" "#cfcaca" "#f5e840" "#0683c9" "#e075b0"
# Read the data for each month separately
apr <- read.csv("C:\\Users\\Куралай\\Downloads\\uber-raw-data-apr14.csv")
may <- read.csv("C:\\Users\\Куралай\\Downloads\\uber-raw-data-may14.csv")
june <- read.csv("C:\\Users\\Куралай\\Downloads\\uber-raw-data-jun14.csv")
july <- read.csv("C:\\Users\\Куралай\\Downloads\\uber-raw-data-jul14.csv")
aug <- read.csv("C:\\Users\\Куралай\\Downloads\\uber-raw-data-aug14.csv")
sept <- read.csv("C:\\Users\\Куралай\\Downloads\\uber-raw-data-sep14.csv")
# Combine the data together
data <- rbind(apr, may, june, july, aug, sept)
cat("The dimensions of the data are:", dim(data))
## The dimensions of the data are: 4534327 4
head(data)
## Date.Time Lat Lon Base
## 1 4/1/2014 0:11:00 40.7690 -73.9549 B02512
## 2 4/1/2014 0:17:00 40.7267 -74.0345 B02512
## 3 4/1/2014 0:21:00 40.7316 -73.9873 B02512
## 4 4/1/2014 0:28:00 40.7588 -73.9776 B02512
## 5 4/1/2014 0:33:00 40.7594 -73.9722 B02512
## 6 4/1/2014 0:33:00 40.7383 -74.0403 B02512
data$Date.Time <- as.POSIXct(data$Date.Time, format="%m/%d/%Y %H:%M:%S")
# Создание столбца Time с временем из столбца Date.Time
data$Time <- format(as.POSIXct(data$Date.Time, format = "%m/%d/%Y %H:%M:%S"), format="%H:%M:%S")
data$Date.Time <- ymd_hms(data$Date.Time)
## Warning: 2211 failed to parse.
head(data)
## Date.Time Lat Lon Base Time
## 1 2014-04-01 00:11:00 40.7690 -73.9549 B02512 00:11:00
## 2 2014-04-01 00:17:00 40.7267 -74.0345 B02512 00:17:00
## 3 2014-04-01 00:21:00 40.7316 -73.9873 B02512 00:21:00
## 4 2014-04-01 00:28:00 40.7588 -73.9776 B02512 00:28:00
## 5 2014-04-01 00:33:00 40.7594 -73.9722 B02512 00:33:00
## 6 2014-04-01 00:33:00 40.7383 -74.0403 B02512 00:33:00
data$day <- factor(day(data$Date.Time))
data$month <- factor(month(data$Date.Time, label=TRUE))
data$year <- factor(year(data$Date.Time))
data$dayofweek <- factor(wday(data$Date.Time, label=TRUE))
data$second = factor(second(hms(data$Time)))
data$minute = factor(minute(hms(data$Time)))
data$hour = factor(hour(hms(data$Time)))
head(data)
## Date.Time Lat Lon Base Time day month year dayofweek
## 1 2014-04-01 00:11:00 40.7690 -73.9549 B02512 00:11:00 1 апр 2014 вт
## 2 2014-04-01 00:17:00 40.7267 -74.0345 B02512 00:17:00 1 апр 2014 вт
## 3 2014-04-01 00:21:00 40.7316 -73.9873 B02512 00:21:00 1 апр 2014 вт
## 4 2014-04-01 00:28:00 40.7588 -73.9776 B02512 00:28:00 1 апр 2014 вт
## 5 2014-04-01 00:33:00 40.7594 -73.9722 B02512 00:33:00 1 апр 2014 вт
## 6 2014-04-01 00:33:00 40.7383 -74.0403 B02512 00:33:00 1 апр 2014 вт
## second minute hour
## 1 0 11 0
## 2 0 17 0
## 3 0 21 0
## 4 0 28 0
## 5 0 33 0
## 6 0 33 0
hourly_data <- data %>%
group_by(hour) %>%
dplyr::summarize(Total = n())
# Shos data in a searchable js table
datatable(hourly_data)
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# Plot the data by hour
ggplot(hourly_data, aes(hour, Total)) +
geom_bar(stat="identity",
fill="steelblue",
color="red") +
ggtitle("Trips Every Hour", subtitle = "aggregated today") +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)) +
scale_y_continuous(labels=comma)
month_hour_data <- data %>% group_by(month, hour) %>% dplyr::summarize(Total = n())
## `summarise()` has grouped output by 'month'. You can override using the
## `.groups` argument.
ggplot2::ggplot(month_hour_data, aes(hour, Total, fill=month)) +
geom_bar(stat = "identity") +
ggtitle("Trips by Hour and Month") +
scale_y_continuous(labels = comma)
c
## function (...) .Primitive("c")
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.