Q1

#install.packages(rio)
library(rio)

affairs <- import("https://raw.githubusercontent.com/excelsiordata/RBridgeCourse/master/Fair.csv")

print(summary(affairs))
##       V1                sex                 age              ym        
##  Length:601         Length:601         Min.   :17.50   Min.   : 0.125  
##  Class :character   Class :character   1st Qu.:27.00   1st Qu.: 4.000  
##  Mode  :character   Mode  :character   Median :32.00   Median : 7.000  
##                                        Mean   :32.49   Mean   : 8.178  
##                                        3rd Qu.:37.00   3rd Qu.:15.000  
##                                        Max.   :57.00   Max.   :15.000  
##     child             religious       education       occupation   
##  Length:601         Min.   :1.000   Min.   : 9.00   Min.   :1.000  
##  Class :character   1st Qu.:2.000   1st Qu.:14.00   1st Qu.:3.000  
##  Mode  :character   Median :3.000   Median :16.00   Median :5.000  
##                     Mean   :3.116   Mean   :16.17   Mean   :4.195  
##                     3rd Qu.:4.000   3rd Qu.:18.00   3rd Qu.:6.000  
##                     Max.   :5.000   Max.   :20.00   Max.   :7.000  
##       rate         nbaffairs     
##  Min.   :1.000   Min.   : 0.000  
##  1st Qu.:3.000   1st Qu.: 0.000  
##  Median :4.000   Median : 0.000  
##  Mean   :3.932   Mean   : 1.456  
##  3rd Qu.:5.000   3rd Qu.: 0.000  
##  Max.   :5.000   Max.   :12.000
print("Full Set Mean Age:")
## [1] "Full Set Mean Age:"
mean(affairs[["age"]])
## [1] 32.48752
print("Full Set Median Age:")
## [1] "Full Set Median Age:"
median(affairs[["age"]])
## [1] 32
print("Full Set Mean Education:")
## [1] "Full Set Mean Education:"
mean(affairs[["education"]])
## [1] 16.16639
print("Full Set Median Education:")
## [1] "Full Set Median Education:"
median(affairs[["education"]])
## [1] 16
print("Full Set Mean # of Affairs:")
## [1] "Full Set Mean # of Affairs:"
mean(affairs[["nbaffairs"]])
## [1] 1.455907
print("Full Set Median # of Affairs:")
## [1] "Full Set Median # of Affairs:"
median(affairs[["nbaffairs"]])
## [1] 0

Q2

lessthan28 <- subset(affairs, age<28)

print(head(lessthan28))
##    V1    sex age   ym child religious education occupation rate nbaffairs
## 2   2 female  27 4.00    no         4        14          6    4         0
## 5   5   male  22 0.75    no         2        17          6    3         0
## 7   7 female  22 0.75    no         2        12          1    3         0
## 10 10   male  22 1.50    no         4        14          4    5         0
## 12 12   male  27 4.00   yes         4        18          6    4         0
## 14 14 female  22 1.50    no         2        17          5    4         0

Q3

# Rename columns by name
names(lessthan28)[names(lessthan28)=="sex"] <- "LT28sex"
names(lessthan28)[names(lessthan28)=="age"] <- "LT28age"
names(lessthan28)[names(lessthan28)=="ym"] <- "LT28ym"
names(lessthan28)[names(lessthan28)=="child"] <- "LT28child"
names(lessthan28)[names(lessthan28)=="religious"] <- "LT28religious"
names(lessthan28)[names(lessthan28)=="education"] <- "LT28education"
names(lessthan28)[names(lessthan28)=="occupation"] <- "LT28occupation"
names(lessthan28)[names(lessthan28)=="rate"] <- "LT28rate"
names(lessthan28)[names(lessthan28)=="nbaffairs"] <- "LT28nbaffairs"

print(head(lessthan28))
##    V1 LT28sex LT28age LT28ym LT28child LT28religious LT28education
## 2   2  female      27   4.00        no             4            14
## 5   5    male      22   0.75        no             2            17
## 7   7  female      22   0.75        no             2            12
## 10 10    male      22   1.50        no             4            14
## 12 12    male      27   4.00       yes             4            18
## 14 14  female      22   1.50        no             2            17
##    LT28occupation LT28rate LT28nbaffairs
## 2               6        4             0
## 5               6        3             0
## 7               1        3             0
## 10              4        5             0
## 12              6        4             0
## 14              5        4             0

Q4

print(summary(lessthan28))
##       V1              LT28sex             LT28age          LT28ym      
##  Length:276         Length:276         Min.   :17.50   Min.   : 0.125  
##  Class :character   Class :character   1st Qu.:22.00   1st Qu.: 1.500  
##  Mode  :character   Mode  :character   Median :27.00   Median : 4.000  
##                                        Mean   :24.67   Mean   : 3.568  
##                                        3rd Qu.:27.00   3rd Qu.: 4.000  
##                                        Max.   :27.00   Max.   :10.000  
##   LT28child         LT28religious   LT28education   LT28occupation 
##  Length:276         Min.   :1.000   Min.   : 9.00   Min.   :1.000  
##  Class :character   1st Qu.:2.000   1st Qu.:14.00   1st Qu.:3.000  
##  Mode  :character   Median :3.000   Median :16.00   Median :5.000  
##                     Mean   :2.906   Mean   :15.79   Mean   :3.906  
##                     3rd Qu.:4.000   3rd Qu.:17.00   3rd Qu.:5.000  
##                     Max.   :5.000   Max.   :20.00   Max.   :7.000  
##     LT28rate     LT28nbaffairs    
##  Min.   :1.000   Min.   : 0.0000  
##  1st Qu.:4.000   1st Qu.: 0.0000  
##  Median :4.000   Median : 0.0000  
##  Mean   :4.149   Mean   : 0.9203  
##  3rd Qu.:5.000   3rd Qu.: 0.0000  
##  Max.   :5.000   Max.   :12.0000
print("LT28 Set Mean Age:")
## [1] "LT28 Set Mean Age:"
mean(lessthan28[["LT28age"]])
## [1] 24.67391
print("Full Set Mean Age:")
## [1] "Full Set Mean Age:"
mean(affairs[["age"]])
## [1] 32.48752
print("LT28 Set Median Age:")
## [1] "LT28 Set Median Age:"
median(lessthan28[["LT28age"]])
## [1] 27
print("Full Set Median Age:")
## [1] "Full Set Median Age:"
median(affairs[["age"]])
## [1] 32
print("Full Set Mean Education:")
## [1] "Full Set Mean Education:"
mean(affairs[["education"]])
## [1] 16.16639
print("LT28 Set Mean Education:")
## [1] "LT28 Set Mean Education:"
mean(lessthan28[["LT28education"]])
## [1] 15.78986
print("Full Set Median Education:")
## [1] "Full Set Median Education:"
median(affairs[["education"]])
## [1] 16
print("LT28 Set Median Education:")
## [1] "LT28 Set Median Education:"
median(lessthan28[["LT28education"]])
## [1] 16
print("Full Set Mean # of Affairs:")
## [1] "Full Set Mean # of Affairs:"
mean(affairs[["nbaffairs"]])
## [1] 1.455907
print("LT28 Set Mean # of Affairs:")
## [1] "LT28 Set Mean # of Affairs:"
mean(lessthan28[["LT28nbaffairs"]])
## [1] 0.9202899
print("Full Set Median # of Affairs:")
## [1] "Full Set Median # of Affairs:"
median(affairs[["nbaffairs"]])
## [1] 0
print("LT28 Set Median # of Affairs:")
## [1] "LT28 Set Median # of Affairs:"
median(lessthan28[["LT28nbaffairs"]])
## [1] 0

Q5

lessthan28[lessthan28=="0"]<-"Zero"
lessthan28$LT28sex <- gsub("male","m", lessthan28$LT28sex)
lessthan28$LT28sex <- gsub("fem","f", lessthan28$LT28sex)

print(head(lessthan28))
##    V1 LT28sex LT28age LT28ym LT28child LT28religious LT28education
## 2   2       f      27   4.00        no             4            14
## 5   5       m      22   0.75        no             2            17
## 7   7       f      22   0.75        no             2            12
## 10 10       m      22   1.50        no             4            14
## 12 12       m      27   4.00       yes             4            18
## 14 14       f      22   1.50        no             2            17
##    LT28occupation LT28rate LT28nbaffairs
## 2               6        4          Zero
## 5               6        3          Zero
## 7               1        3          Zero
## 10              4        5          Zero
## 12              6        4          Zero
## 14              5        4          Zero