├── activity.zip ├── doc └── instructions.pdf ├── instructions_fig └── sample_panelplot.png ├── PA1_template_files └── figure-html │ ├── unnamed-chunk-10-1.png │ ├── unnamed-chunk-15-1.png │ ├── unnamed-chunk-19-1.png │ ├── unnamed-chunk-6-1.png │ └── unnamed-chunk-9-1.png ├── rsconnect └── documents │ └── PA1_template.Rmd │ └── rpubs.com │ └── rpubs │ └── Publish Document.dcf ├── PA1_template.Rmd ├── README.md └── PA1_template.md /activity.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/RepData_PeerAssessment1/master/activity.zip -------------------------------------------------------------------------------- /doc/instructions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/RepData_PeerAssessment1/master/doc/instructions.pdf -------------------------------------------------------------------------------- /instructions_fig/sample_panelplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/RepData_PeerAssessment1/master/instructions_fig/sample_panelplot.png -------------------------------------------------------------------------------- /PA1_template_files/figure-html/unnamed-chunk-10-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/RepData_PeerAssessment1/master/PA1_template_files/figure-html/unnamed-chunk-10-1.png -------------------------------------------------------------------------------- /PA1_template_files/figure-html/unnamed-chunk-15-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/RepData_PeerAssessment1/master/PA1_template_files/figure-html/unnamed-chunk-15-1.png -------------------------------------------------------------------------------- /PA1_template_files/figure-html/unnamed-chunk-19-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/RepData_PeerAssessment1/master/PA1_template_files/figure-html/unnamed-chunk-19-1.png -------------------------------------------------------------------------------- /PA1_template_files/figure-html/unnamed-chunk-6-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/RepData_PeerAssessment1/master/PA1_template_files/figure-html/unnamed-chunk-6-1.png -------------------------------------------------------------------------------- /PA1_template_files/figure-html/unnamed-chunk-9-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/RepData_PeerAssessment1/master/PA1_template_files/figure-html/unnamed-chunk-9-1.png -------------------------------------------------------------------------------- /rsconnect/documents/PA1_template.Rmd/rpubs.com/rpubs/Publish Document.dcf: -------------------------------------------------------------------------------- 1 | name: Publish Document 2 | account: rpubs 3 | server: rpubs.com 4 | bundleId: 5 | https://api.rpubs.com/api/v1/document/136049/834f1f109a104cfc9be5776c2227d0d3 6 | url: 7 | http://rpubs.com/publish/claim/136049/6f2b4b48f2f14ef38613fd0d6e1ff4b0 8 | when: 1450642438.94937 9 | -------------------------------------------------------------------------------- /PA1_template.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Reproducible Research: Peer Assessment 1" 3 | output: 4 | html_document: 5 | keep_md: true 6 | --- 7 | 8 | ## Import all libraries to be used 9 | ```{r, echo=TRUE} 10 | require(ggplot2) 11 | require(lubridate) 12 | require(gridExtra) 13 | ``` 14 | 15 | ## Loading and preprocessing the data 16 | 17 | To load the data we just need to extract it from the zip file: 18 | ```{r echo=TRUE} 19 | unzip("activity.zip") 20 | ``` 21 | 22 | Then load the csv as follows: 23 | ```{r echo=TRUE} 24 | df <- read.csv("activity.csv") 25 | ``` 26 | 27 | Then, we can take an initial look at the data: 28 | ```{r echo=TRUE} 29 | head(df, 10) 30 | summary(df) 31 | ``` 32 | 33 | 34 | ## What is mean total number of steps taken per day? 35 | First, let's extract the daily data into a different data frame: 36 | 37 | ```{r echo=TRUE} 38 | spd <- data.frame(tapply(df$steps, df$date, FUN = sum)) 39 | names(spd) <- c("steps") 40 | head(spd) 41 | summary(spd) 42 | ``` 43 | 44 | Now let's print a histogram of the number of steps per day: 45 | ```{r echo=TRUE} 46 | ggplot(spd, aes(x=steps)) + geom_histogram(binwidth = 1000) + geom_vline(aes(xintercept=mean(steps, na.rm = TRUE)), color="maroon") + ggtitle("Steps per day frequency") + xlab("Number of steps") + ylab("Frequency") 47 | ``` 48 | 49 | As you can see the mean and median are as follows: 50 | ```{r echo=TRUE} 51 | with(spd, mean(steps, na.rm = TRUE)) 52 | with(spd, median(steps, na.rm = TRUE)) 53 | ``` 54 | 55 | ## What is the average daily activity pattern? 56 | Let's again, prepare a data frame to explore the values: 57 | 58 | ```{r echo=TRUE} 59 | spi <- data.frame(tapply(df$steps, df$interval, FUN = mean, na.rm=TRUE)) 60 | names(spi) <- c("avg") 61 | head(spi) 62 | tail(spi) 63 | summary(spi) 64 | ``` 65 | 66 | Now, let's plot the average steps taken each interval accross the dates: 67 | ```{r echo=TRUE} 68 | ggplot(spi, aes(as.numeric(row.names(spi)), avg)) + geom_point() + geom_line() + ggtitle("Average steps at interval") + xlab("Interval") + ylab("Avg. Steps") 69 | ``` 70 | 71 | We can see that the 5 minute interval in which this subject has the maximum number of steps is: 72 | ```{r echo=TRUE} 73 | spi[spi$avg == max(spi$avg),] 74 | ``` 75 | 76 | ## Imputing missing values 77 | However, the original dataset had lots of NA's that might be changing the result of the analysis. Let's investigate and fix this issue: 78 | ```{r echo=TRUE} 79 | length(df$steps) 80 | sum(is.na(df)) 81 | sum(is.na(df))/length(df$steps)*100 82 | ``` 83 | 84 | About 13% of the values are missing. Let's replace them by the interval's average: 85 | ```{r echo=TRUE} 86 | df$steps2 <- df$steps 87 | df[is.na(df$steps), "steps"] <- spi[,"avg"] 88 | ``` 89 | 90 | Let's create a new dataset with the cleaned values and return the original to the initial state: 91 | ```{r echo=TRUE} 92 | df2 <- df 93 | df$steps <- df$steps2 94 | df$steps2 <- NULL 95 | df2$steps2 <- NULL 96 | head(df) 97 | head(df2) 98 | summary(df) 99 | summary(df2) 100 | any(is.na(df$steps)) 101 | any(is.na(df2$steps)) 102 | ``` 103 | 104 | 105 | Now that we took care of missing values, let's calculate the mean and median once again and see the effect of our changes: 106 | ```{r echo=TRUE} 107 | spd2 <- data.frame(tapply(df2$steps, df2$date, FUN = sum)) 108 | names(spd2) <- c("steps") 109 | head(spd2) 110 | summary(spd2) 111 | ``` 112 | 113 | Now let's print a histogram of the number of steps per day: 114 | ```{r echo=TRUE} 115 | ggplot(spd2, aes(x=steps)) + geom_histogram(binwidth = 1000) + geom_vline(aes(xintercept=mean(steps, na.rm = TRUE)), color="maroon") + ggtitle("Steps per day frequency") + xlab("Number of steps") + ylab("Frequency") 116 | ``` 117 | 118 | As you can see the mean and median are as follows: 119 | ```{r echo=TRUE} 120 | with(spd2, mean(steps, na.rm = TRUE)) 121 | with(spd2, median(steps, na.rm = TRUE)) 122 | ``` 123 | 124 | Surprisingly, the median and mean now are equal. This is the effect of changing the NA's values. 125 | 126 | ## Are there differences in activity patterns between weekdays and weekends? 127 | First, let's add a factor variable representing the weekend and weekdays days: 128 | ```{r echo=TRUE} 129 | df2$weekday <- as.factor(ifelse(wday(as.Date(df2$date)) == 1 | wday(as.Date(df2$date)) == 7, "weekend", "weekday")) 130 | summary(df2) 131 | ``` 132 | 133 | Let's extract the values per weekday: 134 | ```{r echo=TRUE} 135 | wdd <- subset(df2, df2$weekday == "weekend") 136 | wyd <- subset(df2, df2$weekday == "weekday") 137 | wdspi <- data.frame(tapply(wdd$steps, wdd$interval, FUN = mean, na.rm=TRUE)) 138 | wyspi <- data.frame(tapply(wyd$steps, wyd$interval, FUN = mean, na.rm=TRUE)) 139 | names(wdspi) <- c("avg") 140 | names(wyspi) <- c("avg") 141 | head(wdspi) 142 | head(wyspi) 143 | tail(wdspi) 144 | tail(wyspi) 145 | summary(wdspi) 146 | summary(wyspi) 147 | ``` 148 | 149 | Now, let's compare the interval activity per weekday: 150 | ```{r echo=TRUE} 151 | wdplot <- ggplot(wdspi, aes(as.numeric(row.names(wdspi)), avg)) + geom_point() + geom_line() + ggtitle("Weekend Activity") + xlab("Interval") + ylab("Avg. Steps") 152 | wyplot <- ggplot(wyspi, aes(as.numeric(row.names(wyspi)), avg)) + geom_point() + geom_line() + ggtitle("Weekday Activity") + xlab("Interval") + ylab("Avg. Steps") 153 | grid.arrange(wdplot, wyplot, nrow=2) 154 | ``` 155 | 156 | Interestingly, the average number of steps in weekdays is greater than in weekends, however, the weekend max is greater than the weekday max. 157 | 158 | 159 | 160 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Introduction 2 | 3 | It is now possible to collect a large amount of data about personal 4 | movement using activity monitoring devices such as a 5 | [Fitbit](http://www.fitbit.com), [Nike 6 | Fuelband](http://www.nike.com/us/en_us/c/nikeplus-fuelband), or 7 | [Jawbone Up](https://jawbone.com/up). These type of devices are part of 8 | the "quantified self" movement -- a group of enthusiasts who take 9 | measurements about themselves regularly to improve their health, to 10 | find patterns in their behavior, or because they are tech geeks. But 11 | these data remain under-utilized both because the raw data are hard to 12 | obtain and there is a lack of statistical methods and software for 13 | processing and interpreting the data. 14 | 15 | This assignment makes use of data from a personal activity monitoring 16 | device. This device collects data at 5 minute intervals through out the 17 | day. The data consists of two months of data from an anonymous 18 | individual collected during the months of October and November, 2012 19 | and include the number of steps taken in 5 minute intervals each day. 20 | 21 | ## Data 22 | 23 | The data for this assignment can be downloaded from the course web 24 | site: 25 | 26 | * Dataset: [Activity monitoring data](https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip) [52K] 27 | 28 | The variables included in this dataset are: 29 | 30 | * **steps**: Number of steps taking in a 5-minute interval (missing 31 | values are coded as `NA`) 32 | 33 | * **date**: The date on which the measurement was taken in YYYY-MM-DD 34 | format 35 | 36 | * **interval**: Identifier for the 5-minute interval in which 37 | measurement was taken 38 | 39 | 40 | 41 | 42 | The dataset is stored in a comma-separated-value (CSV) file and there 43 | are a total of 17,568 observations in this 44 | dataset. 45 | 46 | 47 | ## Assignment 48 | 49 | This assignment will be described in multiple parts. You will need to 50 | write a report that answers the questions detailed below. Ultimately, 51 | you will need to complete the entire assignment in a **single R 52 | markdown** document that can be processed by **knitr** and be 53 | transformed into an HTML file. 54 | 55 | Throughout your report make sure you always include the code that you 56 | used to generate the output you present. When writing code chunks in 57 | the R markdown document, always use `echo = TRUE` so that someone else 58 | will be able to read the code. **This assignment will be evaluated via 59 | peer assessment so it is essential that your peer evaluators be able 60 | to review the code for your analysis**. 61 | 62 | For the plotting aspects of this assignment, feel free to use any 63 | plotting system in R (i.e., base, lattice, ggplot2) 64 | 65 | Fork/clone the [GitHub repository created for this 66 | assignment](http://github.com/rdpeng/RepData_PeerAssessment1). You 67 | will submit this assignment by pushing your completed files into your 68 | forked repository on GitHub. The assignment submission will consist of 69 | the URL to your GitHub repository and the SHA-1 commit ID for your 70 | repository state. 71 | 72 | NOTE: The GitHub repository also contains the dataset for the 73 | assignment so you do not have to download the data separately. 74 | 75 | 76 | 77 | ### Loading and preprocessing the data 78 | 79 | Show any code that is needed to 80 | 81 | 1. Load the data (i.e. `read.csv()`) 82 | 83 | 2. Process/transform the data (if necessary) into a format suitable for your analysis 84 | 85 | 86 | ### What is mean total number of steps taken per day? 87 | 88 | For this part of the assignment, you can ignore the missing values in 89 | the dataset. 90 | 91 | 1. Make a histogram of the total number of steps taken each day 92 | 93 | 2. Calculate and report the **mean** and **median** total number of steps taken per day 94 | 95 | 96 | ### What is the average daily activity pattern? 97 | 98 | 1. Make a time series plot (i.e. `type = "l"`) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis) 99 | 100 | 2. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps? 101 | 102 | 103 | ### Imputing missing values 104 | 105 | Note that there are a number of days/intervals where there are missing 106 | values (coded as `NA`). The presence of missing days may introduce 107 | bias into some calculations or summaries of the data. 108 | 109 | 1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with `NA`s) 110 | 111 | 2. Devise a strategy for filling in all of the missing values in the dataset. The strategy does not need to be sophisticated. For example, you could use the mean/median for that day, or the mean for that 5-minute interval, etc. 112 | 113 | 3. Create a new dataset that is equal to the original dataset but with the missing data filled in. 114 | 115 | 4. Make a histogram of the total number of steps taken each day and Calculate and report the **mean** and **median** total number of steps taken per day. Do these values differ from the estimates from the first part of the assignment? What is the impact of imputing missing data on the estimates of the total daily number of steps? 116 | 117 | 118 | ### Are there differences in activity patterns between weekdays and weekends? 119 | 120 | For this part the `weekdays()` function may be of some help here. Use 121 | the dataset with the filled-in missing values for this part. 122 | 123 | 1. Create a new factor variable in the dataset with two levels -- "weekday" and "weekend" indicating whether a given date is a weekday or weekend day. 124 | 125 | 1. Make a panel plot containing a time series plot (i.e. `type = "l"`) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all weekday days or weekend days (y-axis). The plot should look something like the following, which was created using **simulated data**: 126 | 127 | ![Sample panel plot](instructions_fig/sample_panelplot.png) 128 | 129 | 130 | **Your plot will look different from the one above** because you will 131 | be using the activity monitor data. Note that the above plot was made 132 | using the lattice system but you can make the same version of the plot 133 | using any plotting system you choose. 134 | 135 | 136 | ## Submitting the Assignment 137 | 138 | To submit the assignment: 139 | 140 | 1. Commit your completed `PA1_template.Rmd` file to the `master` branch of your git repository (you should already be on the `master` branch unless you created new ones) 141 | 142 | 2. Commit your `PA1_template.md` and `PA1_template.html` files produced by processing your R markdown file with the `knit2html()` function in R (from the **knitr** package) 143 | 144 | 3. If your document has figures included (it should) then they should have been placed in the `figure/` directory by default (unless you overrode the default). Add and commit the `figure/` directory to your git repository. 145 | 146 | 4. Push your `master` branch to GitHub. 147 | 148 | 5. Submit the URL to your GitHub repository for this assignment on the course web site. 149 | 150 | In addition to submitting the URL for your GitHub repository, you will 151 | need to submit the 40 character SHA-1 hash (as string of numbers from 152 | 0-9 and letters from a-f) that identifies the repository commit that 153 | contains the version of the files you want to submit. You can do this 154 | in GitHub by doing the following: 155 | 156 | 1. Go into your GitHub repository web page for this assignment 157 | 158 | 2. Click on the "?? commits" link where ?? is the number of commits you have in the repository. For example, if you made a total of 10 commits to this repository, the link should say "10 commits". 159 | 160 | 3. You will see a list of commits that you have made to this repository. The most recent commit is at the very top. If this represents the version of the files you want to submit, then just click the "copy to clipboard" button on the right hand side that should appear when you hover over the SHA-1 hash. Paste this SHA-1 hash into the course web site when you submit your assignment. If you don't want to use the most recent commit, then go down and find the commit you want and copy the SHA-1 hash. 161 | 162 | A valid submission will look something like (this is just an **example**!) 163 | 164 | ```r 165 | https://github.com/rdpeng/RepData_PeerAssessment1 166 | 167 | 7c376cc5447f11537f8740af8e07d6facc3d9645 168 | ``` 169 | -------------------------------------------------------------------------------- /PA1_template.md: -------------------------------------------------------------------------------- 1 | # Reproducible Research: Peer Assessment 1 2 | 3 | ## Import all libraries to be used 4 | 5 | ```r 6 | require(ggplot2) 7 | ``` 8 | 9 | ``` 10 | ## Loading required package: ggplot2 11 | ``` 12 | 13 | ```r 14 | require(lubridate) 15 | ``` 16 | 17 | ``` 18 | ## Loading required package: lubridate 19 | ``` 20 | 21 | ```r 22 | require(gridExtra) 23 | ``` 24 | 25 | ``` 26 | ## Loading required package: gridExtra 27 | ``` 28 | 29 | ## Loading and preprocessing the data 30 | 31 | To load the data we just need to extract it from the zip file: 32 | 33 | ```r 34 | unzip("activity.zip") 35 | ``` 36 | 37 | Then load the csv as follows: 38 | 39 | ```r 40 | df <- read.csv("activity.csv") 41 | ``` 42 | 43 | Then, we can take an initial look at the data: 44 | 45 | ```r 46 | head(df, 10) 47 | ``` 48 | 49 | ``` 50 | ## steps date interval 51 | ## 1 NA 2012-10-01 0 52 | ## 2 NA 2012-10-01 5 53 | ## 3 NA 2012-10-01 10 54 | ## 4 NA 2012-10-01 15 55 | ## 5 NA 2012-10-01 20 56 | ## 6 NA 2012-10-01 25 57 | ## 7 NA 2012-10-01 30 58 | ## 8 NA 2012-10-01 35 59 | ## 9 NA 2012-10-01 40 60 | ## 10 NA 2012-10-01 45 61 | ``` 62 | 63 | ```r 64 | summary(df) 65 | ``` 66 | 67 | ``` 68 | ## steps date interval 69 | ## Min. : 0.00 2012-10-01: 288 Min. : 0.0 70 | ## 1st Qu.: 0.00 2012-10-02: 288 1st Qu.: 588.8 71 | ## Median : 0.00 2012-10-03: 288 Median :1177.5 72 | ## Mean : 37.38 2012-10-04: 288 Mean :1177.5 73 | ## 3rd Qu.: 12.00 2012-10-05: 288 3rd Qu.:1766.2 74 | ## Max. :806.00 2012-10-06: 288 Max. :2355.0 75 | ## NA's :2304 (Other) :15840 76 | ``` 77 | 78 | 79 | ## What is mean total number of steps taken per day? 80 | First, let's extract the daily data into a different data frame: 81 | 82 | 83 | ```r 84 | spd <- data.frame(tapply(df$steps, df$date, FUN = sum)) 85 | names(spd) <- c("steps") 86 | head(spd) 87 | ``` 88 | 89 | ``` 90 | ## steps 91 | ## 2012-10-01 NA 92 | ## 2012-10-02 126 93 | ## 2012-10-03 11352 94 | ## 2012-10-04 12116 95 | ## 2012-10-05 13294 96 | ## 2012-10-06 15420 97 | ``` 98 | 99 | ```r 100 | summary(spd) 101 | ``` 102 | 103 | ``` 104 | ## steps 105 | ## Min. : 41 106 | ## 1st Qu.: 8841 107 | ## Median :10765 108 | ## Mean :10766 109 | ## 3rd Qu.:13294 110 | ## Max. :21194 111 | ## NA's :8 112 | ``` 113 | 114 | Now let's print a histogram of the number of steps per day: 115 | 116 | ```r 117 | ggplot(spd, aes(x=steps)) + geom_histogram(binwidth = 1000) + geom_vline(aes(xintercept=mean(steps, na.rm = TRUE)), color="maroon") + ggtitle("Steps per day frequency") + xlab("Number of steps") + ylab("Frequency") 118 | ``` 119 | 120 | ![](PA1_template_files/figure-html/unnamed-chunk-6-1.png) 121 | 122 | As you can see the mean and median are as follows: 123 | 124 | ```r 125 | with(spd, mean(steps, na.rm = TRUE)) 126 | ``` 127 | 128 | ``` 129 | ## [1] 10766.19 130 | ``` 131 | 132 | ```r 133 | with(spd, median(steps, na.rm = TRUE)) 134 | ``` 135 | 136 | ``` 137 | ## [1] 10765 138 | ``` 139 | 140 | ## What is the average daily activity pattern? 141 | Let's again, prepare a data frame to explore the values: 142 | 143 | 144 | ```r 145 | spi <- data.frame(tapply(df$steps, df$interval, FUN = mean, na.rm=TRUE)) 146 | names(spi) <- c("avg") 147 | head(spi) 148 | ``` 149 | 150 | ``` 151 | ## avg 152 | ## 0 1.7169811 153 | ## 5 0.3396226 154 | ## 10 0.1320755 155 | ## 15 0.1509434 156 | ## 20 0.0754717 157 | ## 25 2.0943396 158 | ``` 159 | 160 | ```r 161 | tail(spi) 162 | ``` 163 | 164 | ``` 165 | ## avg 166 | ## 2330 2.6037736 167 | ## 2335 4.6981132 168 | ## 2340 3.3018868 169 | ## 2345 0.6415094 170 | ## 2350 0.2264151 171 | ## 2355 1.0754717 172 | ``` 173 | 174 | ```r 175 | summary(spi) 176 | ``` 177 | 178 | ``` 179 | ## avg 180 | ## Min. : 0.000 181 | ## 1st Qu.: 2.486 182 | ## Median : 34.113 183 | ## Mean : 37.383 184 | ## 3rd Qu.: 52.835 185 | ## Max. :206.170 186 | ``` 187 | 188 | Now, let's plot the average steps taken each interval accross the dates: 189 | 190 | ```r 191 | ggplot(spi, aes(as.numeric(row.names(spi)), avg)) + geom_point() + geom_line() + ggtitle("Average steps at interval") + xlab("Interval") + ylab("Avg. Steps") 192 | ``` 193 | 194 | ![](PA1_template_files/figure-html/unnamed-chunk-9-1.png) 195 | 196 | We can see that the 5 minute interval in which this subject has the maximum number of steps is: 197 | 198 | ```r 199 | spi[spi$avg == max(spi$avg),] 200 | ``` 201 | 202 | ``` 203 | ## 835 204 | ## 206.1698 205 | ``` 206 | 207 | ## Imputing missing values 208 | However, the original dataset had lots of NA's that might be changing the result of the analysis. Let's investigate and fix this issue: 209 | 210 | ```r 211 | length(df$steps) 212 | ``` 213 | 214 | ``` 215 | ## [1] 17568 216 | ``` 217 | 218 | ```r 219 | sum(is.na(df)) 220 | ``` 221 | 222 | ``` 223 | ## [1] 2304 224 | ``` 225 | 226 | ```r 227 | sum(is.na(df))/length(df$steps)*100 228 | ``` 229 | 230 | ``` 231 | ## [1] 13.11475 232 | ``` 233 | 234 | About 13% of the values are missing. Let's replace them by the interval's average: 235 | 236 | ```r 237 | df$steps2 <- df$steps 238 | df[is.na(df$steps), "steps"] <- spi[,"avg"] 239 | ``` 240 | 241 | Let's create a new dataset with the cleaned values and return the original to the initial state: 242 | 243 | ```r 244 | df2 <- df 245 | df$steps <- df$steps2 246 | df$steps2 <- NULL 247 | df2$steps2 <- NULL 248 | head(df) 249 | ``` 250 | 251 | ``` 252 | ## steps date interval 253 | ## 1 NA 2012-10-01 0 254 | ## 2 NA 2012-10-01 5 255 | ## 3 NA 2012-10-01 10 256 | ## 4 NA 2012-10-01 15 257 | ## 5 NA 2012-10-01 20 258 | ## 6 NA 2012-10-01 25 259 | ``` 260 | 261 | ```r 262 | head(df2) 263 | ``` 264 | 265 | ``` 266 | ## steps date interval 267 | ## 1 1.7169811 2012-10-01 0 268 | ## 2 0.3396226 2012-10-01 5 269 | ## 3 0.1320755 2012-10-01 10 270 | ## 4 0.1509434 2012-10-01 15 271 | ## 5 0.0754717 2012-10-01 20 272 | ## 6 2.0943396 2012-10-01 25 273 | ``` 274 | 275 | ```r 276 | summary(df) 277 | ``` 278 | 279 | ``` 280 | ## steps date interval 281 | ## Min. : 0.00 2012-10-01: 288 Min. : 0.0 282 | ## 1st Qu.: 0.00 2012-10-02: 288 1st Qu.: 588.8 283 | ## Median : 0.00 2012-10-03: 288 Median :1177.5 284 | ## Mean : 37.38 2012-10-04: 288 Mean :1177.5 285 | ## 3rd Qu.: 12.00 2012-10-05: 288 3rd Qu.:1766.2 286 | ## Max. :806.00 2012-10-06: 288 Max. :2355.0 287 | ## NA's :2304 (Other) :15840 288 | ``` 289 | 290 | ```r 291 | summary(df2) 292 | ``` 293 | 294 | ``` 295 | ## steps date interval 296 | ## Min. : 0.00 2012-10-01: 288 Min. : 0.0 297 | ## 1st Qu.: 0.00 2012-10-02: 288 1st Qu.: 588.8 298 | ## Median : 0.00 2012-10-03: 288 Median :1177.5 299 | ## Mean : 37.38 2012-10-04: 288 Mean :1177.5 300 | ## 3rd Qu.: 27.00 2012-10-05: 288 3rd Qu.:1766.2 301 | ## Max. :806.00 2012-10-06: 288 Max. :2355.0 302 | ## (Other) :15840 303 | ``` 304 | 305 | ```r 306 | any(is.na(df$steps)) 307 | ``` 308 | 309 | ``` 310 | ## [1] TRUE 311 | ``` 312 | 313 | ```r 314 | any(is.na(df2$steps)) 315 | ``` 316 | 317 | ``` 318 | ## [1] FALSE 319 | ``` 320 | 321 | 322 | Now that we took care of missing values, let's calculate the mean and median once again and see the effect of our changes: 323 | 324 | ```r 325 | spd2 <- data.frame(tapply(df2$steps, df2$date, FUN = sum)) 326 | names(spd2) <- c("steps") 327 | head(spd2) 328 | ``` 329 | 330 | ``` 331 | ## steps 332 | ## 2012-10-01 10766.19 333 | ## 2012-10-02 126.00 334 | ## 2012-10-03 11352.00 335 | ## 2012-10-04 12116.00 336 | ## 2012-10-05 13294.00 337 | ## 2012-10-06 15420.00 338 | ``` 339 | 340 | ```r 341 | summary(spd2) 342 | ``` 343 | 344 | ``` 345 | ## steps 346 | ## Min. : 41 347 | ## 1st Qu.: 9819 348 | ## Median :10766 349 | ## Mean :10766 350 | ## 3rd Qu.:12811 351 | ## Max. :21194 352 | ``` 353 | 354 | Now let's print a histogram of the number of steps per day: 355 | 356 | ```r 357 | ggplot(spd2, aes(x=steps)) + geom_histogram(binwidth = 1000) + geom_vline(aes(xintercept=mean(steps, na.rm = TRUE)), color="maroon") + ggtitle("Steps per day frequency") + xlab("Number of steps") + ylab("Frequency") 358 | ``` 359 | 360 | ![](PA1_template_files/figure-html/unnamed-chunk-15-1.png) 361 | 362 | As you can see the mean and median are as follows: 363 | 364 | ```r 365 | with(spd2, mean(steps, na.rm = TRUE)) 366 | ``` 367 | 368 | ``` 369 | ## [1] 10766.19 370 | ``` 371 | 372 | ```r 373 | with(spd2, median(steps, na.rm = TRUE)) 374 | ``` 375 | 376 | ``` 377 | ## [1] 10766.19 378 | ``` 379 | 380 | Surprisingly, the median and mean now are equal. This is the effect of changing the NA's values. 381 | 382 | ## Are there differences in activity patterns between weekdays and weekends? 383 | First, let's add a factor variable representing the weekend and weekdays days: 384 | 385 | ```r 386 | df2$weekday <- as.factor(ifelse(wday(as.Date(df2$date)) == 1 | wday(as.Date(df2$date)) == 7, "weekend", "weekday")) 387 | summary(df2) 388 | ``` 389 | 390 | ``` 391 | ## steps date interval weekday 392 | ## Min. : 0.00 2012-10-01: 288 Min. : 0.0 weekday:12960 393 | ## 1st Qu.: 0.00 2012-10-02: 288 1st Qu.: 588.8 weekend: 4608 394 | ## Median : 0.00 2012-10-03: 288 Median :1177.5 395 | ## Mean : 37.38 2012-10-04: 288 Mean :1177.5 396 | ## 3rd Qu.: 27.00 2012-10-05: 288 3rd Qu.:1766.2 397 | ## Max. :806.00 2012-10-06: 288 Max. :2355.0 398 | ## (Other) :15840 399 | ``` 400 | 401 | Let's extract the values per weekday: 402 | 403 | ```r 404 | wdd <- subset(df2, df2$weekday == "weekend") 405 | wyd <- subset(df2, df2$weekday == "weekday") 406 | wdspi <- data.frame(tapply(wdd$steps, wdd$interval, FUN = mean, na.rm=TRUE)) 407 | wyspi <- data.frame(tapply(wyd$steps, wyd$interval, FUN = mean, na.rm=TRUE)) 408 | names(wdspi) <- c("avg") 409 | names(wyspi) <- c("avg") 410 | head(wdspi) 411 | ``` 412 | 413 | ``` 414 | ## avg 415 | ## 0 0.214622642 416 | ## 5 0.042452830 417 | ## 10 0.016509434 418 | ## 15 0.018867925 419 | ## 20 0.009433962 420 | ## 25 3.511792453 421 | ``` 422 | 423 | ```r 424 | head(wyspi) 425 | ``` 426 | 427 | ``` 428 | ## avg 429 | ## 0 2.25115304 430 | ## 5 0.44528302 431 | ## 10 0.17316562 432 | ## 15 0.19790356 433 | ## 20 0.09895178 434 | ## 25 1.59035639 435 | ``` 436 | 437 | ```r 438 | tail(wdspi) 439 | ``` 440 | 441 | ``` 442 | ## avg 443 | ## 2330 1.38797170 444 | ## 2335 11.58726415 445 | ## 2340 6.28773585 446 | ## 2345 1.70518868 447 | ## 2350 0.02830189 448 | ## 2355 0.13443396 449 | ``` 450 | 451 | ```r 452 | tail(wyspi) 453 | ``` 454 | 455 | ``` 456 | ## avg 457 | ## 2330 3.0360587 458 | ## 2335 2.2486373 459 | ## 2340 2.2402516 460 | ## 2345 0.2633124 461 | ## 2350 0.2968553 462 | ## 2355 1.4100629 463 | ``` 464 | 465 | ```r 466 | summary(wdspi) 467 | ``` 468 | 469 | ``` 470 | ## avg 471 | ## Min. : 0.000 472 | ## 1st Qu.: 1.241 473 | ## Median : 32.340 474 | ## Mean : 42.366 475 | ## 3rd Qu.: 74.654 476 | ## Max. :166.639 477 | ``` 478 | 479 | ```r 480 | summary(wyspi) 481 | ``` 482 | 483 | ``` 484 | ## avg 485 | ## Min. : 0.000 486 | ## 1st Qu.: 2.247 487 | ## Median : 25.803 488 | ## Mean : 35.611 489 | ## 3rd Qu.: 50.854 490 | ## Max. :230.378 491 | ``` 492 | 493 | Now, let's compare the interval activity per weekday: 494 | 495 | ```r 496 | wdplot <- ggplot(wdspi, aes(as.numeric(row.names(wdspi)), avg)) + geom_point() + geom_line() + ggtitle("Weekend Activity") + xlab("Interval") + ylab("Avg. Steps") 497 | wyplot <- ggplot(wyspi, aes(as.numeric(row.names(wyspi)), avg)) + geom_point() + geom_line() + ggtitle("Weekday Activity") + xlab("Interval") + ylab("Avg. Steps") 498 | grid.arrange(wdplot, wyplot, nrow=2) 499 | ``` 500 | 501 | ![](PA1_template_files/figure-html/unnamed-chunk-19-1.png) 502 | 503 | Interestingly, the average number of steps in weekdays is greater than in weekends, however, the weekend max is greater than the weekday max. 504 | 505 | 506 | 507 | --------------------------------------------------------------------------------