# House Keeping:
rm(list = ls())
setwd("/Users/bmishra/Dropbox/OSU/PhD/Fall 2021/STAT5193 SAS R/R/Assignments/Homework 3")
# Assignment 3:
# Q1 (a):
Txtsent = c(1, 10, 150, 18, 30, 100, 20, 100, 150,
75, 75, 50, 25, 100, 30, 5, 20, 200,
50, 10, 100, 100, 30, 10, 30, 10, 9,
100,11, 2, 10, 5, 10, 5, 25)
Txtrec = c(1, 15, 150, 28, 30, 75, 20, 100, 150,
75, 75, 50, 30, 100, 30, 5, 40, 200,
50, 10, 100, 100, 30, 10, 30, 20, 3,
200, 15, 3, 10, 5, 15, 5, 25)
Introvert = c(8, 8, 1, 4, 4, 6, 5, 6, 3, 5, 5,
3.5, 8, 5, 5, 8, 3, 3, 8, 3, 1,
4, 7, 5, 7, 5, 5, 6, 3, 4, 3, 2, 6, 3, 7)
Snapchat = c("Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N",
"Y", "Y", "N", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "N", "Y",
"Y", "Y", "N", "N", "N", "N", "N", "N", "N")
# Q1(b):
# Txtsent and Txtrec: Yes
mode(Txtsent) == mode(Txtrec)
## [1] TRUE
# Txtsent and Introvert: Yes
mode(Txtsent) == mode(Introvert)
## [1] TRUE
# Txtsent and Snapchat: No
mode(Txtsent) == mode(Snapchat)
## [1] FALSE
# Txtrec and Introvert: Yes
mode(Txtrec) == mode(Introvert)
## [1] TRUE
# Txtrec and Snapchat: No
mode(Txtrec) == mode(Snapchat)
## [1] FALSE
# Introvert and Snapchat: No
mode(Introvert) == mode(Snapchat)
## [1] FALSE
# Introvert and Snapchat: No
identical(mode(Introvert), mode(Snapchat))
## [1] FALSE
# Q1(c): The mode of variables above are not same. Txtrec, Txtsent and Introvert have numeric mode but Snapchat is character. Matrix needs same mode. So, we cannot store these variables as a matrix; we need to store as dataframe.
# Q1 (d):
# Note: I don't have three innitials.
BM = cbind(Txtsent, Txtrec, Introvert, Snapchat)
mode(BM[[4]])
## [1] "character"
# Data Frames:
# Q1 (e):
summary(BM)
## Txtsent Txtrec Introvert Snapchat
## 10 : 6 30 : 5 5 :8 N:11
## 100 : 6 100 : 4 3 :7 Y:24
## 30 : 4 10 : 3 8 :5
## 5 : 3 15 : 3 4 :4
## 150 : 2 5 : 3 6 :4
## 20 : 2 75 : 3 7 :3
## (Other):12 (Other):14 (Other):4
# Snapchat is character variable. So, it has "Y" or "N" which means "Yes" or "No" respectively. There are 11 students with Y means 11 students are using snapchat and 24 students are not using snapchat.
# Q1 (f):
help("data.frame")
BM1 = data.frame(cbind(Txtsent, Txtrec, Introvert, Snapchat),
stringsAsFactors = T)
str(BM1)
## 'data.frame': 35 obs. of 4 variables:
## $ Txtsent : Factor w/ 15 levels "1","10","100",..: 1 2 5 6 11 3 8 3 5 14 ...
## $ Txtrec : Factor w/ 15 levels "1","10","100",..: 1 4 5 9 11 15 6 3 5 15 ...
## $ Introvert: Factor w/ 9 levels "1","2","3","3.5",..: 9 9 1 5 5 7 6 7 3 6 ...
## $ Snapchat : Factor w/ 2 levels "N","Y": 2 2 2 2 2 2 2 2 2 2 ...
# Q1 (g):
summary(BM1)
## Txtsent Txtrec Introvert Snapchat
## 10 : 6 30 : 5 5 :8 N:11
## 100 : 6 100 : 4 3 :7 Y:24
## 30 : 4 10 : 3 8 :5
## 5 : 3 15 : 3 4 :4
## 150 : 2 5 : 3 6 :4
## 20 : 2 75 : 3 7 :3
## (Other):12 (Other):14 (Other):4
mode(BM1$Snapchat)
## [1] "numeric"
# Factors:
# Q2 (a.i):
set.seed(1)
# Q2 (a.ii):
x <- rpois(25, 1)
x.f = factor(x, ordered = T)
x.f
## [1] 0 1 1 2 0 2 3 1 1 0 0 0 1 1 2 1 1 4 1 2 3 0 1 0 0
## Levels: 0 < 1 < 2 < 3 < 4
# Q2 (b.i):
help("table")
# Fill in the blank โtable uses the cross-classifying factors to build a contingency table of the counts at each combination of factor levels.
# Q2 (b.ii):
table(x.f)
## x.f
## 0 1 2 3 4
## 8 10 4 2 1
plot(x, x.f)

# Q3:
# Q3 (i):
my.name = "Bijesh"
# Q3 (ii):
purpose = "Structure and mode Matter"
# Q3 (iii):
summary1 = summary(BM)
summary2 = summary(BM1)
what.I.Learned = list(my.name, purpose, summary1, summary2)
# Q3 (iv):
what.I.Learned
## [[1]]
## [1] "Bijesh"
##
## [[2]]
## [1] "Structure and mode Matter"
##
## [[3]]
## Txtsent Txtrec Introvert Snapchat
## 10 : 6 30 : 5 5 :8 N:11
## 100 : 6 100 : 4 3 :7 Y:24
## 30 : 4 10 : 3 8 :5
## 5 : 3 15 : 3 4 :4
## 150 : 2 5 : 3 6 :4
## 20 : 2 75 : 3 7 :3
## (Other):12 (Other):14 (Other):4
##
## [[4]]
## Txtsent Txtrec Introvert Snapchat
## 10 : 6 30 : 5 5 :8 N:11
## 100 : 6 100 : 4 3 :7 Y:24
## 30 : 4 10 : 3 8 :5
## 5 : 3 15 : 3 4 :4
## 150 : 2 5 : 3 6 :4
## 20 : 2 75 : 3 7 :3
## (Other):12 (Other):14 (Other):4
save.image("HW3.RData")
load("HW3.RData")