#———————————————————————————– # Hw1: Import data from statistical package and from Database management system

Installation of packages open the files from statistical packages

install.packages(c(“haven”,“readxl”,“readr”))

## Load the packages

library(haven)
library(readxl)
library(readr)
## Load the data # Excel format

excel_data <-read_excel("C:/Users/enzabihimana/OneDrive - SanlamAllianz/Documents/R/R-first-project/student_marks.xlsx")
## summarizing the data 
head(excel_data)
## # A tibble: 6 × 10
##   student_id student_name gender   age  math english science history geography
##        <dbl> <chr>        <chr>  <dbl> <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
## 1          1 Kelvin Ardan M         17    78      65      72      80        69
## 2          2 Mira Solen   F         16    88      91      85      79        90
## 3          3 Tobias Renk  M         18    54      60      48      66        58
## 4          4 Lena Vorik   F         17    92      87      94      85        88
## 5          5 Dario Pell   M         16    67      72      61      70        75
## 6          6 Nora Beshti  F         17    73      80      77      84        81
## # ℹ 1 more variable: attendance_pct <dbl>
tail(excel_data)
## # A tibble: 6 × 10
##   student_id student_name gender   age  math english science history geography
##        <dbl> <chr>        <chr>  <dbl> <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
## 1          5 Dario Pell   M         16    67      72      61      70        75
## 2          6 Nora Beshti  F         17    73      80      77      84        81
## 3          7 Sami Orlan   M         18    45      52      40      58        49
## 4          8 Ayla Merrow  F         16    81      76      83      72        79
## 5          9 Jonas Tarek  M         17    60      68      55      63        66
## 6         10 Brina Kolt   F         18    95      89      97      92        94
## # ℹ 1 more variable: attendance_pct <dbl>
summary(excel_data$science)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    40.0    56.5    74.5    71.2    84.5    97.0
## Load the data # CSV format

csv_data <- read_csv("C:/Users/enzabihimana/OneDrive - SanlamAllianz/Documents/R/R-first-project/student_marks.csv",show_col_types=FALSE)
## Summarize the data
head(csv_data)
## # A tibble: 6 × 10
##   student_id student_name gender   age  math english science history geography
##        <dbl> <chr>        <chr>  <dbl> <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
## 1          1 Kelvin Ardan M         17    78      65      72      80        69
## 2          2 Mira Solen   F         16    88      91      85      79        90
## 3          3 Tobias Renk  M         18    54      60      48      66        58
## 4          4 Lena Vorik   F         17    92      87      94      85        88
## 5          5 Dario Pell   M         16    67      72      61      70        75
## 6          6 Nora Beshti  F         17    73      80      77      84        81
## # ℹ 1 more variable: attendance_pct <dbl>
tail(csv_data)
## # A tibble: 6 × 10
##   student_id student_name gender   age  math english science history geography
##        <dbl> <chr>        <chr>  <dbl> <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
## 1          5 Dario Pell   M         16    67      72      61      70        75
## 2          6 Nora Beshti  F         17    73      80      77      84        81
## 3          7 Sami Orlan   M         18    45      52      40      58        49
## 4          8 Ayla Merrow  F         16    81      76      83      72        79
## 5          9 Jonas Tarek  M         17    60      68      55      63        66
## 6         10 Brina Kolt   F         18    95      89      97      92        94
## # ℹ 1 more variable: attendance_pct <dbl>
summary(csv_data$science)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    40.0    56.5    74.5    71.2    84.5    97.0
## Load the data # Stata format

dta_data <- read_dta("C:/Users/enzabihimana/OneDrive - SanlamAllianz/Documents/R/R-first-project/student_marks.dta")
## Summarize the data
head(dta_data)
## # A tibble: 6 × 10
##   student_id student_name gender   age  math english science history geography
##        <dbl> <chr>        <chr>  <dbl> <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
## 1          1 Kelvin Ardan M         17    78      65      72      80        69
## 2          2 Mira Solen   F         16    88      91      85      79        90
## 3          3 Tobias Renk  M         18    54      60      48      66        58
## 4          4 Lena Vorik   F         17    92      87      94      85        88
## 5          5 Dario Pell   M         16    67      72      61      70        75
## 6          6 Nora Beshti  F         17    73      80      77      84        81
## # ℹ 1 more variable: attendance_pct <dbl>
tail(dta_data)
## # A tibble: 6 × 10
##   student_id student_name gender   age  math english science history geography
##        <dbl> <chr>        <chr>  <dbl> <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
## 1          5 Dario Pell   M         16    67      72      61      70        75
## 2          6 Nora Beshti  F         17    73      80      77      84        81
## 3          7 Sami Orlan   M         18    45      52      40      58        49
## 4          8 Ayla Merrow  F         16    81      76      83      72        79
## 5          9 Jonas Tarek  M         17    60      68      55      63        66
## 6         10 Brina Kolt   F         18    95      89      97      92        94
## # ℹ 1 more variable: attendance_pct <dbl>
summary(dta_data$science)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    40.0    56.5    74.5    71.2    84.5    97.0

#——————————————————————————-

Hw2: Class activities practice

Vector: The foundational R data strucure

## Examples 
# Example 1

age<-c(25,20,16)
summary(age)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   16.00   18.00   20.00   20.33   22.50   25.00
# Example 2
age <-c(35,24,15,27)
mean(age)
## [1] 25.25
## Character

x <- c("a","b","c")
class<-c("M","F","F","M")

## Logical
x <-c(TRUE,FALSE)
x <-c(T,F)

## Complex
x<-c (1+0i,2+4i)

## Integer
x <- 9:29
x
##  [1]  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
## Matrix and operation with in matrices
(x<-matrix(1:6,nrow=2,ncol=3))
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
(y<-matrix(1:6,nrow=3,ncol=2))
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
x%*%y
##      [,1] [,2]
## [1,]   22   49
## [2,]   28   64
### Exploratory data Analysis

dta_data
## # A tibble: 10 × 10
##    student_id student_name gender   age  math english science history geography
##         <dbl> <chr>        <chr>  <dbl> <dbl>   <dbl>   <dbl>   <dbl>     <dbl>
##  1          1 Kelvin Ardan M         17    78      65      72      80        69
##  2          2 Mira Solen   F         16    88      91      85      79        90
##  3          3 Tobias Renk  M         18    54      60      48      66        58
##  4          4 Lena Vorik   F         17    92      87      94      85        88
##  5          5 Dario Pell   M         16    67      72      61      70        75
##  6          6 Nora Beshti  F         17    73      80      77      84        81
##  7          7 Sami Orlan   M         18    45      52      40      58        49
##  8          8 Ayla Merrow  F         16    81      76      83      72        79
##  9          9 Jonas Tarek  M         17    60      68      55      63        66
## 10         10 Brina Kolt   F         18    95      89      97      92        94
## # ℹ 1 more variable: attendance_pct <dbl>
summary(dta_data)
##    student_id       student_name       gender        age             math      
##  Min.   : 1.00   Length   :10    Length   :10   Min.   :16.00   Min.   :45.00  
##  1st Qu.: 3.25   N.unique :10    N.unique : 2   1st Qu.:16.25   1st Qu.:61.75  
##  Median : 5.50   N.blank  : 0    N.blank  : 0   Median :17.00   Median :75.50  
##  Mean   : 5.50   Min.nchar:10    Min.nchar: 1   Mean   :17.00   Mean   :73.30  
##  3rd Qu.: 7.75   Max.nchar:12    Max.nchar: 1   3rd Qu.:17.75   3rd Qu.:86.25  
##  Max.   :10.00                                  Max.   :18.00   Max.   :95.00  
##     english         science        history       geography     attendance_pct  
##  Min.   :52.00   Min.   :40.0   Min.   :58.0   Min.   :49.00   Min.   : 75.00  
##  1st Qu.:65.75   1st Qu.:56.5   1st Qu.:67.0   1st Qu.:66.75   1st Qu.: 86.25  
##  Median :74.00   Median :74.5   Median :75.5   Median :77.00   Median : 94.00  
##  Mean   :74.00   Mean   :71.2   Mean   :74.9   Mean   :74.90   Mean   : 91.30  
##  3rd Qu.:85.25   3rd Qu.:84.5   3rd Qu.:83.0   3rd Qu.:86.25   3rd Qu.: 97.50  
##  Max.   :91.00   Max.   :97.0   Max.   :92.0   Max.   :94.00   Max.   :100.00
str(dta_data,give.attr=FALSE)
## tibble [10 × 10] (S3: tbl_df/tbl/data.frame)
##  $ student_id    : num [1:10] 1 2 3 4 5 6 7 8 9 10
##  $ student_name  : chr [1:10] "Kelvin Ardan" "Mira Solen" "Tobias Renk" "Lena Vorik" ...
##  $ gender        : chr [1:10] "M" "F" "M" "F" ...
##  $ age           : num [1:10] 17 16 18 17 16 17 18 16 17 18
##  $ math          : num [1:10] 78 88 54 92 67 73 45 81 60 95
##  $ english       : num [1:10] 65 91 60 87 72 80 52 76 68 89
##  $ science       : num [1:10] 72 85 48 94 61 77 40 83 55 97
##  $ history       : num [1:10] 80 79 66 85 70 84 58 72 63 92
##  $ geography     : num [1:10] 69 90 58 88 75 81 49 79 66 94
##  $ attendance_pct: num [1:10] 95 98 82 100 90 93 75 96 85 99
names(dta_data)
##  [1] "student_id"     "student_name"   "gender"         "age"           
##  [5] "math"           "english"        "science"        "history"       
##  [9] "geography"      "attendance_pct"
categ_anal=table(Gender=dta_data$gender,Grade=dta_data$science)
round(prop.table(categ_anal,1)*100,1)
##       Grade
## Gender 40 48 55 61 72 77 83 85 94 97
##      F  0  0  0  0  0 20 20 20 20 20
##      M 20 20 20 20 20  0  0  0  0  0

Exploratory Data Analysis using built in data in R

Examine your variables

data("women")
women
##    height weight
## 1      58    115
## 2      59    117
## 3      60    120
## 4      61    123
## 5      62    126
## 6      63    129
## 7      64    132
## 8      65    135
## 9      66    139
## 10     67    142
## 11     68    146
## 12     69    150
## 13     70    154
## 14     71    159
## 15     72    164
names(women)
## [1] "height" "weight"
dim(women)
## [1] 15  2

Summary Statistics

# Summary Statistics
summary(women)
##      height         weight     
##  Min.   :58.0   Min.   :115.0  
##  1st Qu.:61.5   1st Qu.:124.5  
##  Median :65.0   Median :135.0  
##  Mean   :65.0   Mean   :136.7  
##  3rd Qu.:68.5   3rd Qu.:148.0  
##  Max.   :72.0   Max.   :164.0
str(women,give.attr=FALSE)
## 'data.frame':    15 obs. of  2 variables:
##  $ height: num  58 59 60 61 62 63 64 65 66 67 ...
##  $ weight: num  115 117 120 123 126 129 132 135 139 142 ...
apply(women,1,mean)
##  [1]  86.5  88.0  90.0  92.0  94.0  96.0  98.0 100.0 102.5 104.5 107.0 109.5
## [13] 112.0 115.0 118.0
apply(women,2,sd)
##    height    weight 
##  4.472136 15.498694
c(mean(women$weight),sd(women$weight))
## [1] 136.73333  15.49869
c(mean(women$height),sd(women$height))
## [1] 65.000000  4.472136
c(Mean=mean(women$height),SD=sd(women$height))
##      Mean        SD 
## 65.000000  4.472136
c(Mean=mean(women$weight),SD=sd(women$weight))
##      Mean        SD 
## 136.73333  15.49869

Variation & Co-variation using both Numerical & graphs

Relationship between categorical and continuous variables

data("iris")
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
mean(iris$Sepal.Length)
## [1] 5.843333
# Mean of Petal.length by setosa category
mean(iris$Petal.Length[iris$Species=="setosa"])
## [1] 1.462
# Mean of Petal.length by Versicolor category
mean(iris$Petal.Length[iris$Species=="versicolor"])
## [1] 4.26
# Mean of Petal.length by virginica category
mean(iris$Petal.Length[iris$Species=="virginica"])
## [1] 5.552
# Examine the mean by Shortcut
by(iris$Petal.Length,iris$Species,mean)
## iris$Species: setosa
## [1] 1.462
## ------------------------------------------------------------ 
## iris$Species: versicolor
## [1] 4.26
## ------------------------------------------------------------ 
## iris$Species: virginica
## [1] 5.552
# Examine the SD by Shortcut
by(iris$Petal.Length,iris$Species,sd)
## iris$Species: setosa
## [1] 0.173664
## ------------------------------------------------------------ 
## iris$Species: versicolor
## [1] 0.469911
## ------------------------------------------------------------ 
## iris$Species: virginica
## [1] 0.5518947
# Examine summary by Shortcut
by(iris$Petal.Length,iris$Species,summary)
## iris$Species: setosa
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   1.400   1.500   1.462   1.575   1.900 
## ------------------------------------------------------------ 
## iris$Species: versicolor
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    3.00    4.00    4.35    4.26    4.60    5.10 
## ------------------------------------------------------------ 
## iris$Species: virginica
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   4.500   5.100   5.550   5.552   5.875   6.900

Visualize the differences in continouus variables btn categoarical using Box_and_Whisker plot

boxplot(iris$Sepal.Length~iris$Species,data = iris,main="Comparison of Categorical VS Quantitative")

## Relationship btn two categorical variables

ucba<-data.frame(UCBAdmissions)
head(ucba)
##      Admit Gender Dept Freq
## 1 Admitted   Male    A  512
## 2 Rejected   Male    A  313
## 3 Admitted Female    A   89
## 4 Rejected Female    A   19
## 5 Admitted   Male    B  353
## 6 Rejected   Male    B  207
# Cross tabulation # Contigency table
(cross<-xtabs(Freq~Gender+Admit,data = ucba))
##         Admit
## Gender   Admitted Rejected
##   Male       1198     1493
##   Female      557     1278
# Is there gender bias in UCB graduate admission process?
prop.table(cross,1)
##         Admit
## Gender    Admitted  Rejected
##   Male   0.4451877 0.5548123
##   Female 0.3035422 0.6964578

Simpson’s paradox

Phenomenon, where a trend that appears in combined groups of data disappears or reverses when broken down into groups.

cross2<-xtabs(Freq~Gender+Admit,data = ucba[ucba$Dept=="F",])
prop.table(cross2,1)
##         Admit
## Gender     Admitted   Rejected
##   Male   0.05898123 0.94101877
##   Female 0.07038123 0.92961877

Bar Chart

data1<-read.table(text = "ProdA ProdB ProdC ProdD
                  1 110 50 60 70
                  2 120 50 80 65",header= TRUE)
barplot(as.matrix(data1),beside=TRUE,col=c("red","green"))

#barplot(as.matrix(data2),beside=TRUE,col=c("Gold3","red"))
data2<- read.table(text = "A B C D E F G
                   1 10 80 30 90 70 60 90
                   2 20 50 70 50 40 10 40
                   3 60 80 80 60 60 30 1560
                   4 20 40 70 80 20 10 70",header=TRUE)
barplot(as.matrix(data2))

# Scatter Plot

plot(weight~height,data = women)

plot(women$weight,women$height)

plot(women[,1],women[,2],main = "Graph")

# Linear Model

plot(weight~height,data = women)
abline(lm(weight~height,data = women)$coefficient)

data_lm<-lm(height~weight,data = women)
s<-summary(data_lm)
round(s$coefficients,5)
##             Estimate Std. Error  t value Pr(>|t|)
## (Intercept) 25.72346    1.04375 24.64531        0
## weight       0.28725    0.00759 37.85531        0
plot(data_lm)

hist(women$height,col = "green")

hist(women$weight,col = "darkblue")

# Linear with other line

plot(weight~height,data = women)
abline(lm(weight~height,data = women)$coefficient)
lines(lowess(women$height,women$height),col="red")

cor(women$height,women$weight)
## [1] 0.9954948
lm(weight~height,women)
## 
## Call:
## lm(formula = weight ~ height, data = women)
## 
## Coefficients:
## (Intercept)       height  
##      -87.52         3.45

Data Management

Create another Variable

# Convert pounds to kilogram and Inches to meters
BMI<-(women$weight*0.453592)/(women$height*0.0254)^2
round(BMI,1)
##  [1] 24.0 23.6 23.4 23.2 23.0 22.9 22.7 22.5 22.4 22.2 22.2 22.2 22.1 22.2 22.2
# attach it to data frame and get new data frame
women$BMI <-round(703*women$weight/women$height^2,1)
head(women)
##   height weight  BMI
## 1     58    115 24.0
## 2     59    117 23.6
## 3     60    120 23.4
## 4     61    123 23.2
## 5     62    126 23.0
## 6     63    129 22.8
summary(women$BMI)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   22.10   22.20   22.50   22.71   23.10   24.00
# Summary of BMI
summary(women$BMI)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   22.10   22.20   22.50   22.71   23.10   24.00
stem(BMI,scale = 2)
## 
##   The decimal point is 1 digit(s) to the left of the |
## 
##   220 | 058
##   222 | 044
##   224 | 36
##   226 | 6
##   228 | 5
##   230 | 5
##   232 | 4
##   234 | 4
##   236 | 3
##   238 | 
##   240 | 3

Date

today<- Sys.Date()
format(today,format="%B %d %Y")
## [1] "November 14 2026"
format(today,format = "%A")
## [1] "Saturday"
today<-Sys.Date()
dob<-as.Date("1998-11-06")
#today<-as.Date()
difftime(today,dob,units = "days")
## Time difference of 10235 days
startdate<-as.Date("2022-11-01")
enddate<-as.Date("2026-11-14")
days<-enddate-startdate
days
## Time difference of 1474 days

Subsetting

Creation of leadership data Interest: How men and women differ in the way they lead their organizations. 5 questions were asked in this study. Example of the question: Do men and women in management position differ in the degree to which they defer to superiors? 1: strongly disagree, 2: disagree, 3:neither agree nor disagree,4: agree, 5: strongly agree

# Use of $,[] or [[]]
manager <- c(1,2,3,4,5)
country<- c("US","US","UK","UK","UK")
gender<-c("M","F","F","M","F")
age<-c(32,45,25,39,99)
q1<-c(5,3,3,3,2)
q2<-c(4,5,5,3,2)
q3<-c(5,2,5,4,1)
q4<-c(5,5,5,NA,2)
q5<-c(5,5,2,NA,1)
leadership<-data.frame(manager,country, gender, age, q1,q2,q3,q4,q5, stringsAsFactors = TRUE)
leadership<-data.frame(manager,country, gender, age, q1,q2,q3,q4,q5)
str(leadership)
## 'data.frame':    5 obs. of  9 variables:
##  $ manager: num  1 2 3 4 5
##  $ country: chr  "US" "US" "UK" "UK" ...
##  $ gender : chr  "M" "F" "F" "M" ...
##  $ age    : num  32 45 25 39 99
##  $ q1     : num  5 3 3 3 2
##  $ q2     : num  4 5 5 3 2
##  $ q3     : num  5 2 5 4 1
##  $ q4     : num  5 5 5 NA 2
##  $ q5     : num  5 5 2 NA 1
names(leadership)
## [1] "manager" "country" "gender"  "age"     "q1"      "q2"      "q3"     
## [8] "q4"      "q5"

Missing Value

leadership$age[leadership$age==99]<-NA
View(leadership)
leadership$agecat[leadership$age>75]<-"Elder"
leadership$agecat[leadership$age>=5 & leadership$age<=75]<-"Middle aged"
leadership$agecat[leadership$age<35]<-"Young"

One way of handling missing value

Deleting all observations with missing data ( Listwise deletion) is one of the several methods of handling incomplete datasets. Note: You can also replacing the missing value by the average of the remaining data.

is.na(leadership[,5:9])
##         q1    q2    q3    q4    q5
## [1,] FALSE FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE  TRUE  TRUE
## [5,] FALSE FALSE FALSE FALSE FALSE
new_leadersh<-na.omit(leadership)
new_leadersh
##   manager country gender age q1 q2 q3 q4 q5      agecat
## 1       1      US      M  32  5  4  5  5  5       Young
## 2       2      US      F  45  3  5  2  5  5 Middle aged
## 3       3      UK      F  25  3  5  5  5  2       Young

Sorting Data

order(leadership$age)
## [1] 3 1 4 2 5
new_leadersh2<-leadership[order(leadership$age)]

merging Data-sets

To merge two data frame horizontally, you use merge() function. In most cases, two data frames are joined by one or more common key variables. -example 1: merge(dataframeA, dataframeB,by=“ID”) -example 2: merge(dataframeA,dataframeB, by=c(“ID”,“country”))

the second merge the two dataframes by ID and country

To join two data frame( datasets) vertically, use rbind() function: Note that the two data set must have the same variables

Subset (selecting variables,dropping variables,selecting observation )

(new_ledersh3<-leadership[,c(5:9)])
##   q1 q2 q3 q4 q5
## 1  5  4  5  5  5
## 2  3  5  2  5  5
## 3  3  5  5  5  2
## 4  3  3  4 NA NA
## 5  2  2  1  2  1
new_leadersh4<-leadership[,c(-1,-2)]
(new_leadersh4<-leadership[,-(1:5)])
##   q2 q3 q4 q5      agecat
## 1  4  5  5  5       Young
## 2  5  2  5  5 Middle aged
## 3  5  5  5  2       Young
## 4  3  4 NA NA Middle aged
## 5  2  1  2  1        <NA>
new_leadersh5<-leadership[,c(-1,-7)]
leadership[[4]]
## [1] 32 45 25 39 NA
new_leadersh6<-leadership[c(-1,-3),]
new_leadersh6<-leadership[c(2,4,5),]
new_leadersh7<-subset(leadership,age>=35|age<24,select=c(q1,q2,q3,q4,q5))
attach(leadership)
## The following objects are masked _by_ .GlobalEnv:
## 
##     age, country, gender, manager, q1, q2, q3, q4, q5
(new_leadersh7<-leadership[gender=="M" & age>30,])
##   manager country gender age q1 q2 q3 q4 q5      agecat
## 1       1      US      M  32  5  4  5  5  5       Young
## 4       4      UK      M  39  3  3  4 NA NA Middle aged