#———————————————————————————– # 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)
## tibble [10 × 10] (S3: tbl_df/tbl/data.frame)
##  $ student_id    : num [1:10] 1 2 3 4 5 6 7 8 9 10
##   ..- attr(*, "label")= chr "Student ID"
##   ..- attr(*, "format.stata")= chr "%12.0g"
##  $ student_name  : chr [1:10] "Kelvin Ardan" "Mira Solen" "Tobias Renk" "Lena Vorik" ...
##   ..- attr(*, "label")= chr "Student name (random)"
##   ..- attr(*, "format.stata")= chr "%12s"
##  $ gender        : chr [1:10] "M" "F" "M" "F" ...
##   ..- attr(*, "label")= chr "Gender (M/F)"
##   ..- attr(*, "format.stata")= chr "%1s"
##  $ age           : num [1:10] 17 16 18 17 16 17 18 16 17 18
##   ..- attr(*, "label")= chr "Age in years"
##   ..- attr(*, "format.stata")= chr "%12.0g"
##  $ math          : num [1:10] 78 88 54 92 67 73 45 81 60 95
##   ..- attr(*, "label")= chr "Mathematics mark"
##   ..- attr(*, "format.stata")= chr "%12.0g"
##  $ english       : num [1:10] 65 91 60 87 72 80 52 76 68 89
##   ..- attr(*, "label")= chr "English mark"
##   ..- attr(*, "format.stata")= chr "%12.0g"
##  $ science       : num [1:10] 72 85 48 94 61 77 40 83 55 97
##   ..- attr(*, "label")= chr "Science mark"
##   ..- attr(*, "format.stata")= chr "%12.0g"
##  $ history       : num [1:10] 80 79 66 85 70 84 58 72 63 92
##   ..- attr(*, "label")= chr "History mark"
##   ..- attr(*, "format.stata")= chr "%12.0g"
##  $ geography     : num [1:10] 69 90 58 88 75 81 49 79 66 94
##   ..- attr(*, "label")= chr "Geography mark"
##   ..- attr(*, "format.stata")= chr "%12.0g"
##  $ attendance_pct: num [1:10] 95 98 82 100 90 93 75 96 85 99
##   ..- attr(*, "label")= chr "Attendance (%)"
##   ..- attr(*, "format.stata")= chr "%12.0g"
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