options(repos = c(CRAN = "https://cloud.r-project.org"))
install.packages(c("rmarkdown", "haven", "DBI", "RMariaDB", "jsonlite"))
## package 'rmarkdown' successfully unpacked and MD5 sums checked
## package 'haven' successfully unpacked and MD5 sums checked
## package 'DBI' successfully unpacked and MD5 sums checked
## package 'RMariaDB' successfully unpacked and MD5 sums checked
## package 'jsonlite' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\KABSTORE\AppData\Local\Temp\RtmpcvHtRP\downloaded_packages
library(rmarkdown)
library(haven)
library(DBI)
library(RMariaDB)
library(jsonlite)

R Markdown

Importing Data from Statistical Software and Databases

R allows users to import data from statistical software and databases. It provides various functions and packages for accessing, reading, and working with data from different sources.

install.packages("rmarkdown")
library(rmarkdown)
library(knitr)

  1. Importing data from statistical packages

library(haven)
getwd()
## [1] "C:/Users/KABSTORE/Documents/R"
file_path <- "C:/Users/KABSTORE/Documents/R"

Stata data

data<- read_dta("household_survey.dta")
View(data)
head(data)
## # A tibble: 6 × 8
##   hh_id district      sex          age education   household_size monthly_income
##   <dbl> <dbl+lbl>     <dbl+lbl>  <dbl> <dbl+lbl>            <dbl>          <dbl>
## 1  1001 1 [Gatsibo]   2 [Female]    75 1 [Primary]              8          87186
## 2  1002 3 [Kayonza]   2 [Female]    64 1 [Primary]              5          76849
## 3  1003 2 [Nyagatare] 2 [Female]    47 1 [Primary]              2         124251
## 4  1004 2 [Nyagatare] 2 [Female]    38 0 [None]                 7          37977
## 5  1005 2 [Nyagatare] 1 [Male]      71 0 [None]                 5          69862
## 6  1006 3 [Kayonza]   1 [Male]      18 2 [Seconda…              5          85146
## # ℹ 1 more variable: has_health_insurance <dbl+lbl>

summary of stata file data

summary(data)
##      hh_id         district        sex             age          education    
##  Min.   :1001   Min.   :1.0   Min.   :1.000   Min.   :18.00   Min.   :0.000  
##  1st Qu.:1051   1st Qu.:1.0   1st Qu.:1.000   1st Qu.:35.00   1st Qu.:1.000  
##  Median :1100   Median :2.0   Median :1.000   Median :48.00   Median :1.000  
##  Mean   :1100   Mean   :2.3   Mean   :1.475   Mean   :48.68   Mean   :1.335  
##  3rd Qu.:1150   3rd Qu.:3.0   3rd Qu.:2.000   3rd Qu.:64.00   3rd Qu.:2.000  
##  Max.   :1200   Max.   :4.0   Max.   :2.000   Max.   :78.00   Max.   :3.000  
##                                               NAs    :5                      
##  household_size monthly_income   has_health_insurance
##  Min.   :1.00   Min.   : 10000   Min.   :0.00        
##  1st Qu.:3.00   1st Qu.: 66693   1st Qu.:1.00        
##  Median :5.50   Median : 84914   Median :1.00        
##  Mean   :5.34   Mean   : 86433   Mean   :0.79        
##  3rd Qu.:7.00   3rd Qu.:108696   3rd Qu.:1.00        
##  Max.   :9.00   Max.   :151550   Max.   :1.00        
##                 NAs    :8

CSV file

data_csv<-read.csv("student.csv")
View(data_csv)

Get Top rows

head(data_csv)
##   student_id               name gender age   faculty year math stats
## 1      S1001   Claire Niyonzima Female  25 Education    2   55    82
## 2      S1002 Claire Nsengiyumva Female  20   Nursing    2   76    45
## 3      S1003      Grace Uwimana Female  25  Business    3   47    66
## 4      S1004   Claire Niyonzima Female  26 Education    3   46    38
## 5      S1005    Diane Niyonzima Female  24        IT    2   91    59
## 6      S1006      Mary Habimana Female  24 Education    1   84    32
##   programming attendance_pct average result
## 1          97             66    78.0   Pass
## 2          53             78    58.0   Pass
## 3          89             93    67.3   Pass
## 4          95             84    59.7   Pass
## 5          71             61    73.7   Pass
## 6          40             92    52.0   Pass

Summary of csv file

summary(data_csv)
##      student_id         name           gender         age       
##  Length   :100   Length   :100   Length   :100   Min.   :18.00  
##  N.unique :100   N.unique : 79   N.unique :  2   1st Qu.:20.00  
##  N.blank  :  0   N.blank  :  0   N.blank  :  0   Median :23.00  
##  Min.nchar:  5   Min.nchar: 10   Min.nchar:  4   Mean   :22.58  
##  Max.nchar:  5   Max.nchar: 20   Max.nchar:  6   3rd Qu.:25.00  
##                                                  Max.   :27.00  
##                                                                 
##       faculty         year           math           stats        programming   
##  Length   :100   Min.   :1.00   Min.   :36.00   Min.   :32.00   Min.   :30.00  
##  N.unique :  4   1st Qu.:1.75   1st Qu.:47.75   1st Qu.:48.75   1st Qu.:48.25  
##  N.blank  :  0   Median :3.00   Median :62.50   Median :68.00   Median :67.00  
##  Min.nchar:  2   Mean   :2.57   Mean   :65.40   Mean   :68.13   Mean   :65.22  
##  Max.nchar:  9   3rd Qu.:4.00   3rd Qu.:84.00   3rd Qu.:87.00   3rd Qu.:82.25  
##                  Max.   :4.00   Max.   :99.00   Max.   :99.00   Max.   :97.00  
##                                                                                
##  attendance_pct     average            result   
##  Min.   :61.00   Min.   :39.70   Length   :100  
##  1st Qu.:71.00   1st Qu.:57.00   N.unique :  2  
##  Median :78.00   Median :67.00   N.blank  :  0  
##  Mean   :79.77   Mean   :66.25   Min.nchar:  4  
##  3rd Qu.:90.00   3rd Qu.:74.08   Max.nchar:  4  
##  Max.   :99.00   Max.   :92.00                  
##  NAs    :4

  1. Importing data from a Database Management System (DBMS)

A Database Management System (DBMS) organizes and stores structured data in tables. R can connect to a database, run SQL queries to retrieve specific information, and import the results into a data frame for analysis.

Common examples of database management systems include:

MySQL PostgreSQL Microsoft SQL Server Oracle Database *SQLite

Importing data from MySQL

library(DBI) library(RMariaDB)

con <- dbConnect(RMariaDB::MariaDB(), host = "localhost", user = "root", password = "")

create a small sample database

dbExecute(con, "CREATE DATABASE IF NOT EXISTS PayrollDB")
## [1] 0
dbExecute(con, "USE PayrollDB")
## [1] 0
dbExecute(con, "DROP TABLE IF EXISTS employees")
## [1] 0
dbExecute(con, "DROP TABLE IF EXISTS departments")
## [1] 0
dbExecute(con, "DROP TABLE IF EXISTS positions")
## [1] 0
dbExecute(con, "CREATE TABLE departments (
  DepartmentID INT PRIMARY KEY,
  DepartmentName VARCHAR(50))")
## [1] 0
dbExecute(con, "CREATE TABLE positions (
  PositionID INT PRIMARY KEY,
  PositionName VARCHAR(50))")
## [1] 0
dbExecute(con, "CREATE TABLE employees (
  EmployeeID INT PRIMARY KEY,
  DepartmentID INT,
  PositionID INT,
  EmploymentDate DATE,
  Status VARCHAR(20))")
## [1] 0
dbExecute(con, "INSERT INTO departments VALUES
  (1,'Finance'),(2,'Human Resources'),(3,'IT')")
## [1] 3
dbExecute(con, "INSERT INTO positions VALUES
  (1,'Accountant'),(2,'HR Officer'),(3,'Developer'),(4,'Manager')")
## [1] 4
dbExecute(con, "INSERT INTO employees VALUES
  (101,1,1,'2022-03-15','Active'),
  (102,2,2,'2021-07-01','Active'),
  (103,3,3,'2023-01-10','Active'),
  (104,3,4,'2020-05-20','Inactive'),
  (105,1,4,'2019-11-02','Active'),
  (106,2,2,'2024-02-12','Active')")
## [1] 6

Check the database was created

Sys.setenv(MYSQL_PWD = "")

con <- dbConnect(
  RMariaDB::MariaDB(),
  dbname   = "PayrollDB",
  host     = "127.0.0.1",
  port     = 3306,
  user     = "root",
  password = Sys.getenv("MYSQL_PWD")
)

employees <- dbGetQuery(con, "
  SELECT e.EmployeeID, d.DepartmentName, p.PositionName,
         e.EmploymentDate, e.Status
  FROM payrolldb.employees e
  JOIN payrolldb.positions p   ON p.PositionID   = e.PositionID
  JOIN payrolldb.departments d ON d.DepartmentID = e.DepartmentID
")

dbDisconnect(con)
head(employees)
##   EmployeeID  DepartmentName PositionName EmploymentDate   Status
## 1        101         Finance   Accountant     2022-03-15   Active
## 2        102 Human Resources   HR Officer     2021-07-01   Active
## 3        103              IT    Developer     2023-01-10   Active
## 4        104              IT      Manager     2020-05-20 Inactive
## 5        105         Finance      Manager     2019-11-02   Active
## 6        106 Human Resources   HR Officer     2024-02-12   Active

  1. Import data from a public API

library(jsonlite)
users<-fromJSON("https://jsonplaceholder.typicode.com/users")
dim(users)
## [1] 10  8
head(users)
##   id                 name         username                     email
## 1  1        Leanne Graham             Bret         Sincere@april.biz
## 2  2         Ervin Howell        Antonette         Shanna@melissa.tv
## 3  3     Clementine Bauch         Samantha        Nathan@yesenia.net
## 4  4     Patricia Lebsack         Karianne Julianne.OConner@kory.org
## 5  5     Chelsey Dietrich           Kamren  Lucio_Hettinger@annie.ca
## 6  6 Mrs. Dennis Schulist Leopoldo_Corkery   Karley_Dach@jasper.info
##      address.street address.suite  address.city address.zipcode address.geo.lat
## 1       Kulas Light      Apt. 556   Gwenborough      92998-3874        -37.3159
## 2     Victor Plains     Suite 879   Wisokyburgh      90566-7771        -43.9509
## 3 Douglas Extension     Suite 847 McKenziehaven      59590-4157        -68.6102
## 4       Hoeger Mall      Apt. 692   South Elvis      53919-4257         29.4572
## 5      Skiles Walks     Suite 351    Roscoeview           33263        -31.8129
## 6 Norberto Crossing      Apt. 950 South Christy      23505-1337        -71.4197
##   address.geo.lng                 phone       website       company.name
## 1         81.1496 1-770-736-8031 x56442 hildegard.org    Romaguera-Crona
## 2        -34.4618   010-692-6593 x09125 anastasia.net       Deckow-Crist
## 3        -47.0653        1-463-123-4447   ramiro.info Romaguera-Jacobson
## 4       -164.2990     493-170-9623 x156      kale.biz      Robel-Corkery
## 5         62.5342         (254)954-1289  demarco.info        Keebler LLC
## 6         71.7478  1-477-935-8478 x6430       ola.org  Considine-Lockman
##                        company.catchPhrase                           company.bs
## 1   Multi-layered client-server neural-net          harness real-time e-markets
## 2           Proactive didactic contingency     synergize scalable supply-chains
## 3        Face to face bifurcated interface      e-enable strategic applications
## 4 Multi-tiered zero tolerance productivity transition cutting-edge web services
## 5     User-centric fault-tolerant solution     revolutionize end-to-end systems
## 6       Synchronised bottom-line interface     e-enable innovative applications
View(users)

Introduction

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Important definitions in data science

Statistics Modelling Computation Calculus

Today’s class, we will study

Installation of R Installation of R studio Calculator in R Data importation in R Data description using numerical measures and graphs.

Data structure (Vectors, Factors, Lists, Data frame, matrix and arrays)

Vector: The foundational R data structure

Numeric

Example 1

x<-c(0.5,0.6)
age<-c(20,35,32,29)
summary(age)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   20.00   26.75   30.50   29.00   32.75   35.00
print(x)
## [1] 0.5 0.6

Example 2

age<-c(24,35,16,40)
mean(age)
## [1] 28.75

###Logical

x <- c(TRUE, FALSE)
X <- c(T, F)

###Character

x<-c("a","b","c")
class<-c("M","F","F","M")
print(x)
## [1] "a" "b" "c"

###Integer

x<-9:29
print(x)
##  [1]  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

###Complex

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

###Matrix and operation within matrices

(x<- matrix(1:6,nrow = 2, ncol = 3))
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
x
##      [,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

###List

(x<-list(1,"a",TRUE, 1+4i))
## [[1]]
## [1] 1
## 
## [[2]]
## [1] "a"
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] 1+4i
x
## [[1]]
## [1] 1
## 
## [[2]]
## [1] "a"
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] 1+4i

###Factor

(x<-factor(c("yes","yes","no","yes","no")))
## [1] yes yes no  yes no 
## Levels: no yes

create a vector with NAs in it

x<-c(1,2,NA,10,3)

Return a logical vector indicating which elements are NA

is.na(x)
## [1] FALSE FALSE  TRUE FALSE FALSE
x<-c(1,2,4,"NA",5)
bad<-is.na(x)
print(bad)
## [1] FALSE FALSE FALSE FALSE FALSE
x[!bad]
## [1] "1"  "2"  "4"  "NA" "5"

what if there are multiple R objects and you want to take the subset with no missing values in any of those objects?

x<-c(1,3,NA,6,NA,5,7)
y<-c("a","b",NA,"d",NA,"f",NA)
good<-complete.cases(x, y)
good
## [1]  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE

###Coercion

if character is present, in a vector, R convert everything in the vector to character strings.

if a vector only contains logical and numbers,R will convert the logical to numbers,Every True become a 1,and every False becomes 0

sum(c(TRUE,TRUE,FALSE,FALSE,FALSE))
## [1] 2

create data

create data using data frames Data is more general than matrix how?? Because data frame can contains different modes of data(Numeric,character and so on ) Similar to what you can see in SPSS,SAS,…. #let’s create a data frame

studentID<- c(1,2,3,4,5)
gender<-c("F","M","M","F","M")
math_score<-c(18,15,12,9,NA)
it_score<-c(10,13,15,10,16)
scoredata<-data.frame(studentID,gender,math_score,it_score)
scoredata
##   studentID gender math_score it_score
## 1         1      F         18       10
## 2         2      M         15       13
## 3         3      M         12       15
## 4         4      F          9       10
## 5         5      M         NA       16
View(scoredata)

create data from keyboard

steps 1. Create data frame(or matrix) with variable names2. Invoke the text editor in the data objected created at first step

data_class2<-data.frame(height=numeric(0),weight=numeric(0),bmi=numeric(0))
data_class2<-edit(data_class2)

##Data Importation R has some features that can allow you to import data from different sources( it can be text file,spreadsheet,or database) 1.Data from excel 2.Data statistical packages(SAS,SPSS,Stata) 3.Data from Text files(ASCII,XML,Webscraping) 4.Data from database management systems(SQL,MySQL,Oracle,Access)

##HW1:Import data from Statistical package and from Database management systems

data_class <- read.table("C:/Users/KABSTORE/Documents/R/class_data.txt", header = TRUE)
variable.names(data_class)
## [1] "HEIGHT" "WEIGHT"
head(data_class)
##   HEIGHT WEIGHT
## 1    161     50
## 2    155     49
## 3    158     42
## 4    170     65
## 5    160     60
## 6    156     52
tail(data_class)
##    HEIGHT WEIGHT
## 38    169     55
## 39    163     53
## 40    169     60
## 41    167     62
## 42    167     59
## 43    158     49
data_class[10:20,]
##    HEIGHT WEIGHT
## 10    152     46
## 11    167     56
## 12    150     50
## 13    149     40
## 14    156     48
## 15    168     48
## 16    158     50
## 17    152     44
## 18    161     44
## 19    151     43
## 20    158     50
summary(data_class$WEIGHT)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   38.00   45.00   50.00   50.91   58.00   65.00
summary(data_class$HEIGHT)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   146.0   155.5   161.0   160.8   167.0   175.0
length(data_class$WEIGHT)
## [1] 43
data_class[,-1]
##  [1] 50 49 42 65 60 52 59 58 38 46 56 50 40 48 48 50 44 44 43 50 65 49 51 64 38
## [26] 47 38 51 41 63 48 43 59 55 58 40 49 55 53 60 62 59 49
data_class$WEIGHT
##  [1] 50 49 42 65 60 52 59 58 38 46 56 50 40 48 48 50 44 44 43 50 65 49 51 64 38
## [26] 47 38 51 41 63 48 43 59 55 58 40 49 55 53 60 62 59 49

In case you want to use data set built in R

data()  #List of datasets currently available
data("airquality")
variable.names(airquality)
## [1] "Ozone"   "Solar.R" "Wind"    "Temp"    "Month"   "Day"
str(airquality)
## 'data.frame':    153 obs. of  6 variables:
##  $ Ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...
##  $ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
##  $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
##  $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
##  $ Month  : int  5 5 5 5 5 5 5 5 5 5 ...
##  $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...

##Exploration data analysis

###Examine numerical variable using common summary

data_class
##    HEIGHT WEIGHT
## 1     161     50
## 2     155     49
## 3     158     42
## 4     170     65
## 5     160     60
## 6     156     52
## 7     170     59
## 8     165     58
## 9     154     38
## 10    152     46
## 11    167     56
## 12    150     50
## 13    149     40
## 14    156     48
## 15    168     48
## 16    158     50
## 17    152     44
## 18    161     44
## 19    151     43
## 20    158     50
## 21    175     65
## 22    163     49
## 23    167     51
## 24    171     64
## 25    146     38
## 26    159     47
## 27    151     38
## 28    165     51
## 29    149     41
## 30    167     63
## 31    160     48
## 32    154     43
## 33    164     59
## 34    169     55
## 35    166     58
## 36    163     40
## 37    160     49
## 38    169     55
## 39    163     53
## 40    169     60
## 41    167     62
## 42    167     59
## 43    158     49
summary(data_class)
##      HEIGHT          WEIGHT     
##  Min.   :146.0   Min.   :38.00  
##  1st Qu.:155.5   1st Qu.:45.00  
##  Median :161.0   Median :50.00  
##  Mean   :160.8   Mean   :50.91  
##  3rd Qu.:167.0   3rd Qu.:58.00  
##  Max.   :175.0   Max.   :65.00
str(data_class)
## 'data.frame':    43 obs. of  2 variables:
##  $ HEIGHT: int  161 155 158 170 160 156 170 165 154 152 ...
##  $ WEIGHT: int  50 49 42 65 60 52 59 58 38 46 ...

###Summary Statistics

summary(data_class)
##      HEIGHT          WEIGHT     
##  Min.   :146.0   Min.   :38.00  
##  1st Qu.:155.5   1st Qu.:45.00  
##  Median :161.0   Median :50.00  
##  Mean   :160.8   Mean   :50.91  
##  3rd Qu.:167.0   3rd Qu.:58.00  
##  Max.   :175.0   Max.   :65.00
apply(data_class,1,mean)
##  [1] 105.5 102.0 100.0 117.5 110.0 104.0 114.5 111.5  96.0  99.0 111.5 100.0
## [13]  94.5 102.0 108.0 104.0  98.0 102.5  97.0 104.0 120.0 106.0 109.0 117.5
## [25]  92.0 103.0  94.5 108.0  95.0 115.0 104.0  98.5 111.5 112.0 112.0 101.5
## [37] 104.5 112.0 108.0 114.5 114.5 113.0 103.5
apply(data_class,2,mean)
##    HEIGHT    WEIGHT 
## 160.76744  50.90698
c(mean(data_class$HEIGHT),sd(data_class$HEIGHT))
## [1] 160.767442   7.200652
c(mean(data_class$WEIGHT),sd(data_class$WEIGHT))
## [1] 50.906977  7.843147
c(Mean=mean(data_class$HEIGHT),SD=sd(data_class$HEIGHT))
##       Mean         SD 
## 160.767442   7.200652
c(Mean=mean(data_class$WEIGHT),SD=sd(data_class$WEIGHT))
##      Mean        SD 
## 50.906977  7.843147

#Variation and covariation using both numerical and graphs

##Relationship between a categorical and continuous variable

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$Petal.Length)
## [1] 3.758
mean(iris$Petal.Length[iris$Species=="setosa"])
## [1] 1.462
mean(iris$Petal.Length[iris$Species=="versicolor"])
## [1] 4.26
mean(iris$Petal.Length[iris$Species=="virginica"])
## [1] 5.552
##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
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
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 continuous variables between categories using Box-and Whisker plot

boxplot(iris$Petal.Length~iris$Species,data = iris,main="comparison of categorical vs quantitative")

###Relationship between 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<-xtabs(Freq~Gender+Admit,data = ucba)
(cross<-xtabs(Freq~Gender+Admit,data = ucba))
##         Admit
## Gender   Admitted Rejected
##   Male       1198     1493
##   Female      557     1278
##Is there gender bias in UCBA 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

dat<-read.table(text = "ProdA ProdB ProdC ProdD
                1  110 50 60 70
                2  120 50 80 65",header=TRUE)

barplot(as.matrix(dat),beside=TRUE,col=c("Yellow","Green"))

#barplot (as.matrix(dat),beside=TRUE, col=c("gold3","red"))

dat<-read.table(text = "A  B  C  D  E  F  G
1  40  80  30  90  70   90
2  20  50  70  50  40   40
3  60  80  80  60  60  160 
4  70  40  70  80  20  70" , header=TRUE)

barplot(as.matrix(dat))

### Scatter Plot

plot(WEIGHT~HEIGHT,data = data_class)

plot(data_class$HEIGHT,data_class$WEIGHT)

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

cor(data_class$HEIGHT,data_class$WEIGHT)
## [1] 0.8039983

Linear models

plot(WEIGHT~HEIGHT,data = data_class)
abline(lm(WEIGHT~HEIGHT,data = data_class)$coefficient)

data_lm<-lm(HEIGHT~WEIGHT,data = data_class)
summary(data_lm)
## 
## Call:
## lm(formula = HEIGHT ~ WEIGHT, data = data_class)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.0980  -2.7175  -0.0031   3.0092  10.2834 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 123.19115    4.39028  28.060  < 2e-16 ***
## WEIGHT        0.73814    0.08526   8.658 8.44e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.334 on 41 degrees of freedom
## Multiple R-squared:  0.6464, Adjusted R-squared:  0.6378 
## F-statistic: 74.95 on 1 and 41 DF,  p-value: 8.442e-11
plot(data_lm)

hist(data_class$HEIGHT)

hist(data_class$WEIGHT,col = "pink")

##Linear with other line

plot(WEIGHT~HEIGHT,data = data_class)
abline(lm(WEIGHT~HEIGHT,data = data_class)$coefficient)
lines(lowess(data_class$HEIGHT,data_class$WEIGHT),col="blue")

cor(data_class$HEIGHT,data_class$WEIGHT)
## [1] 0.8039983
lm(WEIGHT~HEIGHT,data_class)
## 
## Call:
## lm(formula = WEIGHT ~ HEIGHT, data = data_class)
## 
## Coefficients:
## (Intercept)       HEIGHT  
##    -89.8830       0.8757

Data management

create another variable

attach(data_class)
(BMI<-WEIGHT/(HEIGHT/100)^2)
##  [1] 19.28938 20.39542 16.82423 22.49135 23.43750 21.36752 20.41522 21.30395
##  [9] 16.02294 19.90997 20.07960 22.22222 18.01721 19.72387 17.00680 20.02884
## [17] 19.04432 16.97465 18.85882 20.02884 21.22449 18.44255 18.28678 21.88708
## [25] 17.82698 18.59104 16.66594 18.73278 18.46764 22.58955 18.75000 18.13122
## [33] 21.93635 19.25703 21.04805 15.05514 19.14062 19.25703 19.94806 21.00767
## [41] 22.23099 21.15529 19.62826
(BMI<-round(WEIGHT/(HEIGHT/100)^2,digit=2))
##  [1] 19.29 20.40 16.82 22.49 23.44 21.37 20.42 21.30 16.02 19.91 20.08 22.22
## [13] 18.02 19.72 17.01 20.03 19.04 16.97 18.86 20.03 21.22 18.44 18.29 21.89
## [25] 17.83 18.59 16.67 18.73 18.47 22.59 18.75 18.13 21.94 19.26 21.05 15.06
## [37] 19.14 19.26 19.95 21.01 22.23 21.16 19.63
head(cbind(data_class,BMI),n=5)
##   HEIGHT WEIGHT   BMI
## 1    161     50 19.29
## 2    155     49 20.40
## 3    158     42 16.82
## 4    170     65 22.49
## 5    160     60 23.44
new_data_class<-cbind(data_class,BMI)
tail(cbind(data_class,BMI),n=10)
##    HEIGHT WEIGHT   BMI
## 34    169     55 19.26
## 35    166     58 21.05
## 36    163     40 15.06
## 37    160     49 19.14
## 38    169     55 19.26
## 39    163     53 19.95
## 40    169     60 21.01
## 41    167     62 22.23
## 42    167     59 21.16
## 43    158     49 19.63

##Summary of BMI

summary(BMI)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   15.06   18.45   19.63   19.60   21.11   23.44
stem(BMI,scale = 2)
## 
##   The decimal point is at the |
## 
##   15 | 1
##   15 | 
##   16 | 0
##   16 | 78
##   17 | 00
##   17 | 8
##   18 | 0134
##   18 | 56789
##   19 | 01333
##   19 | 679
##   20 | 000144
##   20 | 
##   21 | 012234
##   21 | 99
##   22 | 22
##   22 | 56
##   23 | 4

##Date

today<-Sys.Date()
format(today,format="%B %d %Y")
## [1] "October 30 2026"
format(today,format="%A")
## [1] "Friday"
today<-Sys.Date()
dob<-as.Date("2001-06-17")

#today<-as.Date("2024-8-25")
difftime(today,dob,units = "days")
## Time difference of 9266 days
startdate <- as.Date("2004-02-13")
enddate   <- as.Date("2011-01-22")
days <- enddate - startdate
days
## Time difference of 2535 days

##Subsetting

###Use of $, []. or [[]] 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

manager<-c(1,2,3,4,5)
country<-c("US","UK","US","US","UK")
gender<-c("M","M","F","F","F")
age<-c(26,35,45,39,80)
q1<-c(5,3,2,3,3)
q2<-c(5,2,4,3,5)
q3<-c(2,5,3,5,2)
q4<-c(5,4,5,NA,3)
q5<-c(5,5,3,NA,2)

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" "UK" "US" "US" ...
##  $ gender : chr  "M" "M" "F" "F" ...
##  $ age    : num  26 35 45 39 80
##  $ q1     : num  5 3 2 3 3
##  $ q2     : num  5 2 4 3 5
##  $ q3     : num  2 5 3 5 2
##  $ q4     : num  5 4 5 NA 3
##  $ q5     : num  5 5 3 NA 2
names(leadership)
## [1] "manager" "country" "gender"  "age"     "q1"      "q2"      "q3"     
## [8] "q4"      "q5"

What you can do - combine the score of the five questions -handle the missing values - create a dataset of what you want -create age group or age categories - 99 indicate the value is missing ##Missing value

leadership$age[leadership$age==80]<-NA
head(leadership)
##   manager country gender age q1 q2 q3 q4 q5
## 1       1      US      M  26  5  5  2  5  5
## 2       2      UK      M  35  3  2  5  4  5
## 3       3      US      F  45  2  4  3  5  3
## 4       4      US      F  39  3  3  5 NA NA
## 5       5      UK      F  NA  3  5  2  3  2
leadership$agecat[leadership$age>75]<-"Elder"
leadership$agecat[leadership$age>=35 & leadership$age<=75]<-"Middle Aged"
leadership$agecat[leadership$age<35]<-"Young"

###One way to handle 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
newdata<-na.omit(leadership)
newdata
##   manager country gender age q1 q2 q3 q4 q5      agecat
## 1       1      US      M  26  5  5  2  5  5       Young
## 2       2      UK      M  35  3  2  5  4  5 Middle Aged
## 3       3      US      F  45  2  4  3  5  3 Middle Aged

##Sorting data

order(leadership$age)
## [1] 1 2 4 3 5
newdata2<-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 )

newdata3<-leadership[,c(5:9)]
myvars<-c("q1","q2","q3","q4","q5")
(newdata3<-leadership[myvars])
##   q1 q2 q3 q4 q5
## 1  5  5  2  5  5
## 2  3  2  5  4  5
## 3  2  4  3  5  3
## 4  3  3  5 NA NA
## 5  3  5  2  3  2
newdata4<-leadership[,c(-1,-2)]
(newdata4<-leadership[,-(1:5)])
##   q2 q3 q4 q5      agecat
## 1  5  2  5  5       Young
## 2  2  5  4  5 Middle Aged
## 3  4  3  5  3 Middle Aged
## 4  3  5 NA NA Middle Aged
## 5  5  2  3  2        <NA>
newdata5<-leadership[,c(-1,-7)]
leadership[[4]]
## [1] 26 35 45 39 NA
leadership$age
## [1] 26 35 45 39 NA
newdata6<-leadership[c(-1,-3),]
newdata6<-leadership[c(2,4,5),]
newdata7<-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
#newdata7<-leadership[gender="M" & age>30,]
(newdata7<-leadership[gender=="M" & age>30,])
##   manager country gender age q1 q2 q3 q4 q5      agecat
## 2       2      UK      M  35  3  2  5  4  5 Middle Aged
(newdata7<-leadership[gender=="F" & age>30,])
##   manager country gender age q1 q2 q3 q4 q5      agecat
## 3       3      US      F  45  2  4  3  5  3 Middle Aged
## 4       4      US      F  39  3  3  5 NA NA Middle Aged
## 5       5      UK      F  NA  3  5  2  3  2        <NA>
detach(leadership)