Assignment

Question 1: Suppose there is data on the number of covid cases in 1 week in January 2019 as follows:

Monday : 20 cases

Tuesday : 3 cases

Wednesday: 15 cases

Thursday : 52 cases

Friday : 30 cases

Saturday : 14 cases

Sunday : 20 cases

1. Create a table with vector, specifically with 2 lines, line 1 is information about the date of that case, line 2 is information about the number of cases

weekday <- c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
cases <- c(20, 3, 15, 52, 30, 14, 20)
dataCovid <- data.frame(weekday, cases)
print(dataCovid)
##     weekday cases
## 1    Monday    20
## 2   Tuesday     3
## 3 Wednesday    15
## 4  Thursday    52
## 5    Friday    30
## 6  Saturday    14
## 7    Sunday    20
View(dataCovid)

2. Calculate the total number of cases in a week

totalcases <- sum(as.numeric(dataCovid[,2]))
print(totalcases)
## [1] 154

3. Average number of cases per day

avgcases <- mean(as.numeric(dataCovid[,2]))
print(avgcases)
## [1] 22

4. Find days with cases > 12 cases

dayOver12 <- weekday[cases > 12]
print(dayOver12)
## [1] "Monday"    "Wednesday" "Thursday"  "Friday"    "Saturday"  "Sunday"

5. Find days with no cases

dayNevercase <- weekday[cases == 0]
print(dayNevercase)
## character(0)

6. Draw a scatter plot and a line chart describing the variation in the number of cases in a week (learn the plot() function)

# Convert weekday to a factor with levels in the order of the days of the week
dataCovid$weekday <- factor(dataCovid$weekday, levels = c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'))

# Scatter plot
plot(dataCovid$weekday, dataCovid$cases, main = "Number of Cases per Week", xlab = "Weekday", ylab = "Cases")

# Line chart
lines(dataCovid$weekday, dataCovid$cases, type = "l", col = "red")