I wrote this code for a company to visualize the attendance of its employees in July 2017. I have a folder "attendance" that containing 13 Excels file, a file for every employee. I'm pretty new to R, and I would like some good advice on how I can improve this code.
---
title: "Attendance"
output: html_document
---
#attendance = the time when the employee attend
#attendance1 = the time when the employee must attend
#departure = the time when the employee depart
#departure1 = the time when the employee must depart
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(readxl)
library(lubridate)
library(plyr)
library(base)
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
average_working_hours <- function(Name){
Name <- read_excel(sprintf("~/attendance/%s.xlsx", Name))
av_wh_Name <- mean((Name$departure + hours(12)) - Name$attendance, na.rm = TRUE, trim = 0.1)
av_wh_Name <- av_wh_Name
}
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
delay <- function(Name) {
Name <- read_excel(sprintf("~/attendance/%s.xlsx", Name))
delay <- Name$attendance - Name$attendance1
delay <- mean(delay , na.rm = TRUE)
units(delay) <- "mins"
delay}
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
over_time <- function(Name){
Name <- read_excel(sprintf("~/attendance/%s.xlsx", Name))
over_time <- Name$departure - Name$departure1
over_time[over_time< 0] <- NA
over_time <- mean(over_time, na.rm = TRUE)
units(over_time) <- "mins"
over_time
}
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
Name <- c("Alaa", "Aya_Magdy", "Aya_Omar", "Esam", "Esraa", "Hassan", "Mohamed", "Mahmoud", "Nermin_Fekry", "Noha", "Norhan", "Eman", "Safaa")
attendance <- data.frame("Name" = Name)
attendance <- mutate(attendance, average_working_hours = lapply(Name, average_working_hours), delay = lapply(Name, delay), over_time = lapply(Name, over_time))
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
attendance <- mutate(attendance, average_working_hours = as.numeric(average_working_hours))
ggplot(attendance, aes(x=attendance$Name,y=attendance$average_working_hours)) +
ggtitle('working hours in July') +
ylab(' working hours') +
geom_bar(stat = 'identity', aes(fill = attendance$average_working_hours > 8)) +
theme_gray() +scale_fill_manual(values=c("slategray3", "slategray4")) + theme(axis.text.x = element_text(angle = 45, hjust = 1, colour = "royalblue4") )
```
##The most hard working employee
```{r echo=FALSE, message=FALSE, warning=FALSE}
print(Name[attendance$average_working_hours == max(attendance$average_working_hours)])
```
##Who works more than 8 hours a day
```{r echo=FALSE, message=FALSE, warning=FALSE}
Name[(attendance$average_working_hours) >= 8]
```
##Who works less than 8 hours a day
```{r echo=FALSE, message=FALSE, warning=FALSE}
Name[(attendance$average_working_hours) < 8]
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
attendance <- mutate(attendance, delay = as.numeric(delay))
library(ggplot2)
ggplot(attendance, aes(x=attendance$Name,y=attendance$delay)) +
ggtitle('Average delay') +
ylab('delay in minutes') +
geom_bar(stat = 'identity', aes(fill = attendance$delay < 30)) +
theme_gray() +scale_fill_manual(values=c("slategray4", "slategray3")) + theme(axis.text.x = element_text(angle = 45, hjust = 1, colour = "royalblue4") )
```
##The most commited
```{r echo=FALSE, message=FALSE, warning=FALSE}
Name[attendance$delay == min(attendance$delay)]
```
##Who delay less than 30 minutes
```{r echo=FALSE, message=FALSE, warning=FALSE}
Name[(attendance$delay <= 30)]
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
attendance <- mutate(attendance, over_time = as.numeric(over_time))
ggplot(attendance, aes(x=attendance$Name,y=attendance$over_time)) +
ggtitle('over time in minutes') +
ylab('over time') +
geom_bar(stat = 'identity', aes(fill = attendance$over_time > 60)) +
theme_gray() +scale_fill_manual(values=c("slategray3", "slategray4")) + theme(axis.text.x = element_text(angle = 45, hjust = 1, colour = "royalblue4") )
```
##Who works overtime more than any other
```{r echo=FALSE, message=FALSE, warning=FALSE, comment= NA}
Name[attendance$over_time == max(attendance$over_time)]
```
##Who works overtime more than 1 hour?
```{r echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
Name[attendance$over_time >= 60]
```