Problem 1

Problem 1.1

knitr::opts_chunk$set(echo = TRUE)
# Problem 1
## Problem 1.1
### Problem 1.1.a
my_first_variable <- 35
### Problem 1.1.b
my_second_variable <- round(my_first_variable /9, digits = 3)
### Problem 1.1.c
print (my_second_variable )
## [1] 3.889
### Problem 1.1.d
my_third_variable <- floor(my_second_variable)
### Problem 1.1.e
print (my_third_variable )
## [1] 3

Problem 1.2

knitr::opts_chunk$set(echo = TRUE)
#Problem 1
## Problem 1.2
### Problem 1.2.a
set.seed(8675309)
### Problem 1.2.b
mean_val <- 500
std_dev <- 10
random_num <- rnorm(20, mean = mean_val, sd = std_dev)
cat("Twenty Random Numbers:\n", random_num, "\n")
## Twenty Random Numbers:
##  490.0342 507.2182 493.8279 520.2939 510.6542 509.8722 500.2745 506.7287 505.7207 509.0368 484.5045 510.2264 501.5008 493.4004 490.0541 519.7246 495.582 490.9936 498.4941 491.7211
plot(random_num, main="20 random nos from a Normal distribution with sd 10 and mean 500",xlab="20 Numbers", ylab="Value of each")

Problem 2

Problem 2.a

knitr::opts_chunk$set(echo = TRUE)
#Problem 2
## Problem 2.a
library(ISwR)
df <-data.frame(stroke)
extractedAge <- df$age
hist(extractedAge)

Problem 2.b

knitr::opts_chunk$set(echo = TRUE)
#Problem 2
## Problem 2.b
myBreaks <- seq(0,100,length=7)
hist(extractedAge, breaks=myBreaks)

Problem 2.c

knitr::opts_chunk$set(echo = TRUE)
#Problem 2
## Problem 2.c
hist(extractedAge, breaks=myBreaks, main = "Age Histogram")

Problem 2.d

knitr::opts_chunk$set(echo = TRUE)
#Problem 2
## Problem 2.d
hist(extractedAge, breaks=myBreaks, main = "Age Histogram", xlab="Age of Patients")

Problem 2.e

knitr::opts_chunk$set(echo = TRUE)
#Problem 2
## Problem 2.e
hist(extractedAge, breaks=myBreaks, main = "Age Histogram", xlab="Age of Patients",col="blue")

Problem 2.f

knitr::opts_chunk$set(echo = TRUE)
#Problem 2
## Problem 2.f
myColors <- c('firebrick2', 'darkorange1', 'goldenrod1', 'palegreen2', 'paleturquoise', 'palevioletred1')
hist(extractedAge, breaks=myBreaks, main = "Age Histogram", xlab="Age of Patients",col=myColors)

Problem 2.g

knitr::opts_chunk$set(echo = TRUE)
#Problem 2
## Problem 2.g
createdColors <- c(1,2,3,4,5,6)
hist(extractedAge, breaks=myBreaks, main = "Age Histogram", xlab="Age of Patients",col=createdColors)

Problem 2.h

knitr::opts_chunk$set(echo = TRUE)
#Problem 2
## Problem 2.h
createdColors <- c(1,2,3,4,5,6)
hist(extractedAge, breaks=myBreaks, main = "Age Histogram", xlab="Age of Patients",col=createdColors, freq=FALSE)

Problem 3

Problem 3.a

knitr::opts_chunk$set(echo = TRUE)
#Problem 3
## Problem 3.a
library(ISwR)
df <-data.frame(stroke)
extractedcoma <- df$coma
threeslots <- (extractedcoma[759:761])
threeslots
## [1] No   <NA> Yes 
## Levels: No Yes
is_na_coma <- is.na(threeslots)
is_na_coma
## [1] FALSE  TRUE FALSE

The print of 759, 760, 761 showed that 760th position is as <NA> and 759th and 760th as avaialble (Yes or No)

The function ‘is.na’ is used for identifying whether the arguments is a ‘NA’ value. The print of is.na function showed 760th as TRUE meaning 760th is NA and 759, 761 as FALSE which means data is NOT NA

Problem 3.b

knitr::opts_chunk$set(echo = TRUE)
#Problem 3
## Problem 3.b

tcomadiab <- table(df$coma, df$diab)
tcomadiab
##      
##        No Yes
##   No  645  91
##   Yes  74   6
length(stroke$coma)
## [1] 829
NAComa <- table(is.na(df$coma))
NAComa
## 
## FALSE  TRUE 
##   825     4
NADiab <- table(is.na(df$diab))

NADiab
## 
## FALSE  TRUE 
##   819    10
bothNA <- table(is.na(df$coma) & is.na(df$diab))
bothNA
## 
## FALSE  TRUE 
##   828     1

The table defined with coma and diab as columns lists 816 observations. In coma column, there are 736 ‘No’ and 80 ‘Yes’; in ‘diab’ column, there are 719 ‘No’ and 97 ‘yes, making the 816 observations. However, the length of ‘stroke$coma’ is 829, 13 more than 816. This is due to NA values. Using the function ‘is.na’ missing or NA values can be identified. There are 4 missing values in ‘stroke$coma’, and 10 missing values in ‘stroke$diab’ which is a total of 14. But the difference being only 13, it means that there should be an observation from the two columns that are both NA as can be see from variable ‘bothNA’ above.

Problem 3.c

knitr::opts_chunk$set(echo = TRUE)
#Problem 3
## Problem 3.c

myFirstTable <- table(df$coma, df$diab, useNA = "always")
myFirstTable
##       
##         No Yes <NA>
##   No   645  91    9
##   Yes   74   6    0
##   <NA>   3   0    1

Now 829 0bservations are reported including the NA values. (645+74+3+91+6+9+1=829, this is exactly equal to length(stroke$coma)

Problem 3.d

knitr::opts_chunk$set(echo = TRUE)
#Problem 3
## Problem 3.d

prop.table(myFirstTable) 
##       
##                 No         Yes        <NA>
##   No   0.778045838 0.109770808 0.010856454
##   Yes  0.089264174 0.007237636 0.000000000
##   <NA> 0.003618818 0.000000000 0.001206273
round(prop.table(myFirstTable), digits = 3)
##       
##           No   Yes  <NA>
##   No   0.778 0.110 0.011
##   Yes  0.089 0.007 0.000
##   <NA> 0.004 0.000 0.001

Problem 3.e

knitr::opts_chunk$set(echo = TRUE)
#Problem 3
## Problem 3.e

round(prop.table(myFirstTable,1), digits=3)
##       
##           No   Yes  <NA>
##   No   0.866 0.122 0.012
##   Yes  0.925 0.075 0.000
##   <NA> 0.750 0.000 0.250
round(prop.table(myFirstTable,2), digits=3)
##       
##           No   Yes  <NA>
##   No   0.893 0.938 0.900
##   Yes  0.102 0.062 0.000
##   <NA> 0.004 0.000 0.100

When the second argument in prop.table function is 1, the sum of the row equals to 1; when the second argument in prop.table function is 2, the sum of the column equals to 1.