install.packages(“rmarkdown”)
setwd("C:/Users/hmon1/Desktop/10A Homework") #this is where you downloaded the HW5.csv file
# load the population for HW5
pop<-read.csv("HW5.csv")
#gets random sample of 150
set.seed(48183130) #use your student ID instead of 12345678
data<-data.frame(X=as.numeric(pop[sample(1:nrow(pop), 150, replace=FALSE),]))
#creates a histogram
histdat = hist(data$X,main="Histogram of X values",xlab="X",ylab="Frequency")
#creates a frequency table
tmpX = c(); tmpf = c() # initialize vectors to save values
for (xi in unique(sort(data$X))){ # for each unique value of X
tmpX = c(tmpX,xi) # save that value to the vector tmpX
tmpf = c(tmpf,sum(data$X == xi)) # save the number of times that value appears in data
# in the vectof tmpf
}
table = data.frame(X=tmpX, f = tmpf) #fill in the table on your homework
#creates a percent column in the frequency table
table$per = round(table$f / sum(table$f)*100,2)
table
## X f per
## 1 0 13 8.67
## 2 2 26 17.33
## 3 4 32 21.33
## 4 6 41 27.33
## 5 8 21 14.00
## 6 10 17 11.33
#computes mean
meanX = round(mean(data$X),2)
meanX
## [1] 5.09
#computes median
medianX = median(data$X)
medianX
## [1] 6
#gets mode
modeX = table$X[table$f==max(table$f)]
modeX
## [1] 6
#computes variance
varX = round(var(data$X),2)
varX
## [1] 8.35
# computes standard deviation
sdX = round(sd(data$X),2)
sdX
## [1] 2.89