if(!require(tidyverse)) install.packages('tidyverse',
repos = 'http://cran.us.r-project.org')
if(!require(lattice)) install.packages('lattice',
repos = 'http://cran.us.r-project.org')
## specify global chunk options
knitr::opts_chunk$set(fig.width = 5, fig.height = 4, dpi = 300,
out.width = '90%', fig.align = 'center',
tidy.opts=list(width.cutoff=60),
tidy=TRUE,
cache = TRUE,
message = FALSE,
warning = FALSE)
Storms and other severe weather events can cause both public health and economic problems for communities and municipalities. Many severe events can result in fatalities, injuries, and property damage, and preventing such outcomes to the extent possible is a key concern.
This project involves exploring the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database. This database tracks characteristics of major storms and weather events in the United States, including when and where they occur, as well as estimates of any fatalities, injuries, and property damage.
The data for this assignment can be downloaded from the course web site:
Dataset: Activity monitoring data [52K]
The variables included in this dataset are:
steps: Number of steps taking in a 5-minute interval (missing values are coded as NA)
date: The date on which the measurement was taken in YYYY-MM-DD format
interval: Identifier for the 5-minute interval in which measurement was taken
The dataset is stored in a comma-separated-value (CSV) file and there are a total of 17,568 observations in this dataset.
Let’s start with data download. We copy link used in project description and assign it to fileurl.
# We copy link used in project description and assign it to
# fileurl
filename <- "repdata_data_activity.zip"
# Controll for already existing files If folder doesn't exist
# proceed with download
if (!file.exists(filename)) {
fileURL <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
download.file(fileURL, filename, method = "curl")
}
# If file exists proceed with unzip
if (!file.exists("Source_Classification_Code.rds")) {
unzip(filename)
}
if (!file.exists("activity.csv")) {
unzip(filename)
}
Now we load our downloaded data:
# Read activity.csv
activity <- read.csv("activity.csv", header = TRUE, sep = ",",
na.strings = "NA")
Let’s see how are data look like:
# A first look to data
head(activity)
## steps date interval
## 1 NA 2012-10-01 0
## 2 NA 2012-10-01 5
## 3 NA 2012-10-01 10
## 4 NA 2012-10-01 15
## 5 NA 2012-10-01 20
## 6 NA 2012-10-01 25
# Let's check structure of dataframe
glimpse(activity)
## Observations: 17,568
## Variables: 3
## $ steps <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA...
## $ date <fct> 2012-10-01, 2012-10-01, 2012-10-01, 2012-10-01, 2012-10-01...
## $ interval <int> 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 100, 105, 11...
# Lets the class for every variable
Next step is to understand variable type and check for missing values:
# Let's check for the number of missing values
sum(is.na(activity))
## [1] 2304
# We check classes of every column inside activity
lapply(activity, class)
## $steps
## [1] "integer"
##
## $date
## [1] "factor"
##
## $interval
## [1] "integer"