Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repositoryresearchassignment #514

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 149 additions & 25 deletions PA1_template.Rmd
Original file line number Diff line number Diff line change
@@ -1,25 +1,149 @@
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---


## Loading and preprocessing the data



## What is mean total number of steps taken per day?



## What is the average daily activity pattern?



## Imputing missing values



## Are there differences in activity patterns between weekdays and weekends?
---
title: "Reproducible Research Project 1"
author: "Dyutit Mohanty"
date: "2024-03-11"
output: html_document
---

# Loading and preprocessing the data

```{r}
filename <- "activity.csv"
data <- read.csv(filename, header=TRUE)
head(data)

```

# What is mean total number of steps taken per day?

### Calculate the total number of steps taken per day
```{r}
data2 <- data[!(is.na(data$steps)), ]
steps_per_day <- aggregate(steps ~ date, data=data2, FUN = sum)

```

### Histogram of the total number of steps taken each day
```{r}
hist(steps_per_day$steps, main="Histogram of the total number of steps taken each day", xlab="Steps", ylab="Frequency")

```

### Calculate and report the mean and median of the total number of steps taken per day
```{r}
mean_steps_per_day <- mean(steps_per_day$steps)
median_steps_per_day <- median(steps_per_day$steps)
mean_steps_per_day
median_steps_per_day
```


# What is the average daily activity pattern?

### Make a time series plot of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)
```{r}
my_mean <- function(x) mean(x, na.rm = TRUE) # R kept throwing an error when "mean" was used as arg so I defined my own mean function
data3 <- aggregate(steps ~ interval, data, FUN=my_mean)
head(data3)

```

```{r}
plot(y=data3$steps,x=data3$interval, type="l", xlab="Intervals", ylab="Steps")

```

### Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?

```{r}
index_max_steps <- which.max(data3$steps)
interval_max_steps <- data3$interval[index_max_steps]
interval_max_steps
max(data3$steps)
```


# Imputing missing values

### Calculate and report the total number of missing values in the dataset (total number of rows with NAs)
```{r}
data4 <- subset(data, is.na(data$steps))
dims <- dim(data4)
nrows_na <- dims[1]
nrows_na
```

### 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.
```{r}
# I am using the mean for the interval
data_nona <- data

for (i in 1:nrow(data_nona)){
if (is.na(data_nona$steps[i])){
interval <- data_nona$interval[i]
index <- which(data3$interval == interval)
mean_val <- data3$steps[index]
data_nona$steps[i] <- mean_val
}
}

head(data_nona)

```


### Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r}
head(data_nona)

```

### 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?
```{r}
data4 <- aggregate(steps ~ date, data_nona, FUN=sum)
hist(data4$steps, main="Histogram of the total number of steps taken each day", xlab="Steps", ylab="Frequency")


mean_steps <- mean(data4$steps)
median_steps <- median(data4$steps)
mean_steps
median_steps
```
### Slight increase in average


# Are there differences in activity patterns between weekdays and weekends?

### 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.
```{r}
data5 <- data_nona
data5$date <- as.Date(data5$date)
data5$day <- weekdays(data5$date)

for (i in 1:nrow(data5)){
if (data5$day[i] %in% c("Saturday", "Sunday")){
data5$weekday[i] <- "weekend"
}
else {
data5$weekday[i] <- "weekday"
}
}

fct_days <- factor(data5$weekday)
summary(fct_days)

```

### Make a panel plot containing a time series plot of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all weekday days or weekend days (y-axis). See the README file in the GitHub repository to see an example of what this plot should look like using simulated data.
```{r}
wknd_data <- data5[data5$weekday == "weekend",]
wkday_data <- data5[data5$weekday == "weekday",]

wknd_data <- aggregate(steps ~ interval, wknd_data, mean)
wkday_data <- aggregate(steps ~ interval, wkday_data, mean)

par(mfrow=c(2, 1))
plot(wknd_data$interval, wknd_data$steps, type="l")
plot(wkday_data$interval, wkday_data$steps, type="l")
```

584 changes: 584 additions & 0 deletions PA1_template.html

Large diffs are not rendered by default.

Loading