Use the summary() function to find the five-number summary(minimum, first quartile, third quartile, and maximum) for vectors, matrices, factors, and data frames.
x1=seq(1,10,2)
class(x1)
## [1] "numeric"
is.vector(x1)
## [1] TRUE
summary(x1)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1 3 5 5 7 9
In this example, \(x1\) is vector. summary(x1) gives five-number summary of x1.
x1=seq(2,20,2)
x3=matrix(x1,5,2)
class(x3)
## [1] "matrix" "array"
x3
## [,1] [,2]
## [1,] 2 12
## [2,] 4 14
## [3,] 6 16
## [4,] 8 18
## [5,] 10 20
summary(x3)
## V1 V2
## Min. : 2 Min. :12
## 1st Qu.: 4 1st Qu.:14
## Median : 6 Median :16
## Mean : 6 Mean :16
## 3rd Qu.: 8 3rd Qu.:18
## Max. :10 Max. :20
In this case, x3 is a matrix. summary(x3) displays five-number summary for the values from each column.
We can use the mean() function to find the percentage specific data.
In a college algebra class, then students’ midterm exam grades are given as follows: 78,83,79,92,85,95,75,65,56,92. Find the percentage of students who will fail the test and who will get an A.
midterm=c(78,83,79,92,85,95,75,65,56,92)
mean(midterm<60)
## [1] 0.1
mean(midterm>90)
## [1] 0.3
10% of the students will fail the test, and 30% will receive an A.
We can use as.table() fucntion to create a factor table from a vector.
A company hires 50 female workers and 60 male workers. Construct a table to show this data.
sex=c(female=50,male=60)
as.table(sex)
## female male
## 50 60
We can also use as.table() function to create a two factor table matrix
A company hires 13 full time female workers, 37 part time female workers, 48 full time male workers and 12 part time male workers. Construct a table to show this data.
job=matrix(c(13,48,37,12),ncol=2, byrow=T)
colnames(job)=c("Female", "Male")
rownames(job)=c("Full Time", "Part Time")
job1=as.table(job)
class(job1)
## [1] "table"
#We can also use the summary() function to test for indepndency of contigency tables.
summary(job1)
## Number of cases in table: 110
## Number of factors: 2
## Test for independence of all factors:
## Chisq = 32.19, df = 1, p-value = 1.395e-08
We can use the quantile() funciton to find the percentile of a data set.
Make a sequence data from 1 to 10 by 1. Find the 50% percentile. Find the first and the third quartiles.
x1=seq(1,10)
quantile(x1, .5)
## 50%
## 5.5
quantile(x1,c(.25,.75))
## 25% 75%
## 3.25 7.75
We can compare these results with the summary() function
summary(x1)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 3.25 5.50 5.50 7.75 10.00
We can find corresponding quantile of the data
x=seq(1,10,1)
mean(x<5)
## [1] 0.4
#Chapter 7 Data Visualization
###7.1.1 Histogram We can use the hist() function to construct a histogram
Make a histogram fro the cars mileage per gallon(mpg) for the data mtcars
data("mtcars")
mpg=mtcars$mpg
hist(mpg, mai="MPG for mtcars Data", xlab = "Mile Per Gallon", ylab= "Number of Cars")
###7.1.2 Bar Graph/Plot
We can use the barplot() function to construct a bar plot.
Make a bar graph showing the gears of cars for the mtcars data
gear=table(mtcars$gear)
barplot(gear, main="Gears of Cars",
ylab="Quantity",
names.arg=c("Three Gears", "Four Gears", "Five Gears"))
We can use the pie() function to sketch a circle graph
parts=c(1333, 1140, 304, 228, 192)
names(parts)=c("China", "India", "US", "Indonesia", "Brazil")
pie(parts, main="Population for the Five most countries", clockwise=T,col=rainbow(5))
We use the stem() function to construct a stem and leaf graph.
The midterm grades for a Math class are listed as follows. 89,92,94,86,85,82,73,65,58,52,64,68,85,97,65,56,68,78,83,55,79,84,86,54,45,68,62,75,63,85,96,69,89
grades=c(89,92,94,86,85,82,73,65,58,52,64,68,85,97,65,56,68,78,83,55,79,84,86,54,45,68,62,75,63,85,96,69,89)
stem(grades)
##
## The decimal point is 1 digit(s) to the right of the |
##
## 4 | 5
## 5 | 24568
## 6 | 234558889
## 7 | 3589
## 8 | 2345556699
## 9 | 2467
stem(grades,scale=2)
##
## The decimal point is 1 digit(s) to the right of the |
##
## 4 | 5
## 5 | 24
## 5 | 568
## 6 | 234
## 6 | 558889
## 7 | 3
## 7 | 589
## 8 | 234
## 8 | 5556699
## 9 | 24
## 9 | 67
We can use the boxplot() function to construct a bos and whisker plot Construct a stem and leaf graph of these data.
Make a box and whisker plot for the cars mileage per gallon(mpg) for the data mtcars
data(mtcars)
mpg=mtcars$mpg
boxplot(mpg, main="Boxplot of Miles Per Gallon", ylab=("Miles Per Gallon"))
#Make a box and whisker plot for each factor level of the gear for the data mtcars
gear=mtcars$gear
boxplot(mpg~gear,main="Mileage for each thpe of cars(Gear)", xlab="Number of Gears", ylab="Miles per Gallon(MPG)", col=c("green","yellow","blue"))
We can use the plot() function to create a scatter diagram.
We are investigating the relationship between miles per gallon(MPG) and the amount of horsepower(HP) produced by car’s engine from mtcars data. Make a scatter diagram displaying the data.
mpg=mtcars$mpg
hp=mtcars$hp
"MPG vs HP"=plot(mpg~hp,
main="MPG vs HP",
xlab="Horsepower",
ylab="Miles per Gallon (mpg)" , pch=c(1,2,3), col=c(1,2,3))
legend(280,28,c("Gear 3", "Gear 4", "Gear 5"),pch=1:3, col=c(1,2,3))
grid()
There are 11 variables in the mtcars. Make a scatter plot matrix for the data.
plot(mtcars)
Professor Johnson lists the midterm scores for his calculus class. The scores are 55,58,65,68,72,78,82,85,87,92,95, and 98. He surveys the number of hours spent for studying per week for each students as follows:0.5,0.6,0.8,0.9,1,1.1,1.5,1.8,2,2.1,2.3,and 2.5.
Make a scatter plot Connect the points with a line.
score=c(55,58,65,68,72,78,82,85,87,92,95,98)
time=c(0.5,0.6,0.8,0.9,1,1.1,1.5,1.8,2,2.1,2.3,2.5)
plot(time,score,type="l",lty=6)
segments(1,60,2,90)
segments(1,70,2,85,lty=2)
text(1,62,"A")
text(2,82,"B")
legend(1.8,70,c("segment line","A","B",lty=c(1,2,3)))
title("Interaction plot with dashed line")
score=c(55,58,65,68,72,78,82,85,87,92,95,98)
time=c(0.5,0.6,0.8,0.9,1,1.1,1.5,1.8,2,2.1,2.3,2.5)
score2=c(53,56,59,69,75,76,81,86,89,91,96,99)
time2=c(0.4,0.7,0.9,0.9,1,1.3,1.5,1.8,2,2.2,2.4,2.8)
range_xlim=range(c(time,time2))
range_ylim=range(c(score,score2))
plot(time,score,type="l",xlim=range_xlim,ylim=range_ylim)
lines(time2,score2,lty=2)
abline(h=60,col="Blue")
abline(v=1,col="Red")
we can use the abline() function to create the line of best fit for simple regression. We plot the scatter diagram for the variables mpg and hp. Using the abline() function, we can find the line of best fit for the linear regression model formed by these two variables.
We are investigating the relationship between miles per gallon (mpg) and the amount of horsepower produced by cars’ engines for the mtcars data.
Make a scatter diagram displaying the data. Make a least-squares line on your scatter diagram.(Hint: using lm() function to get the least=square line )
m1=lm(mpg~hp)
plot(mpg~hp)
abline(m1,col="red")
We can use the coplot() function to create a condition plot. Conditional plot is one of scatter plot for each level of the factor of the variable.
coplot(mpg~hp|factor(gear))
We can us density() function to draw a density plot for the data
Generate 1000 sample data from standard normal and draw the density plot of the generated sample data. ( Hint: rnorm() to generate standard normal data and draw density curve using density() )
x=rnorm(1000,0,1)
plot(density(x))
hist(x,prob=T)
lines(density(x))
#We can use the table() function to create a discrete histogram
gear=mtcars$gear
plot(table(gear))
plot(gear)
#without table()
plot(gear)
#line plot for types of gears
plot(table(gear),tyep = "l")
## Warning in plot.window(...): "tyep" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "tyep" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "tyep" is not a
## graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "tyep" is not a
## graphical parameter
## Warning in box(...): "tyep" is not a graphical parameter
## Warning in title(...): "tyep" is not a graphical parameter
## Warning in axis(...): "tyep" is not a graphical parameter
### 7.2.3 Normal Quantile-Quantile (Q-Q) Plot
We can use Q-Q plot to check our data is following normal distribution or not
x<-rnorm(1000,0,1)
qqnorm(x,main="Q-Q Plot for x")
qqline(x,col="red")
Since x is generated from normal distribution, we are not surprised to see x follows normal. Q-Q line can help to make the decision for the normality assumption.
We can use curve() function to draw a curve.
Draw y=x2 and x domain is [-10, 10]
f1=function(x) x^2
curve(f1,-10,10,main="Plot for X square")
Draw y=√x and x domain [0,10]
f2=function(x)sqrt(x)
curve(f2, 0,10, main="Plot for square root of x")
#### Example 7.14
Draw sine curve and x domain [0,2Ï€]
f3=function(x)sin(x)
curve(f3,0,2*pi, main="Plot for Sine Curve")
The most commonly used trigonometric functions are sin() for sine function, cos() for cosine function, and tan() for tangent function. The most commonly used inverse trigonometric functions are asin() for arcsine function, acos()for arccosine function, and atan() for arctangent function.
Draw a function 2sin(2(x−3)+4) curve and x domain [0,2π]
Please note in R, operation notation must be used in function. For example, if we put 2x instead of 2*x, we will get an error.
f3=function(x) 2*sin(2*(x-3)+4)
curve(f3,0,2*pi,main="Plot for 2sin(2(x-3)+4)")
abline(h=0)
#### Example 7.16
Draw standard normal plot from -3 to 3 We can use dexp() function to draw probability density function(PDF) for exponential distribution.
curve(dnorm,-3,3,main="Plot for Standard Normal")
#### Example 7.17
Draw and expnential distribution from 0 to 3
curve(dexp,0,3,main="Exponential Plot")
## 7.3 Setting Graph
We can use par() function to set the graphs. The syntax is *par(mfrow=c(N,M)). This command will divide the pannel into N rows and M columns.
Draw sine and cosine plots from 0 to 2pi on the same pannel
par(mfrow=c(2,1))
x=seq(0,2*pi,.01)
plot(x,sin(x),main="Plot for Sine Curve", type="l",col=2)
abline(h=0)
plot(x,cos(x),main="Plot for Cosine Curve", type="l",col=3)
abline(h=0)
# 1 x 2
par(mfrow=c(1,2))
x=seq(0,2*pi,.01)
plot(x,sin(x),main="Plot for Sine Curve", type="l",col=2)
abline(h=0)
plot(x,cos(x),main="Plot for Cosine Curve", type="l",col=3)
abline(h=0)
### 7.3.2 Writing Graph to a File
There are many types of files that we can save the graphs to it. The most commonly used are BMP, JPEG, PDF and PostScript file.
bmp(file="test1.bmp")
curve(dnorm,-3,3)
dev.off()
## png
## 2
jpeg(file="test1.jpg")
curve(dnorm,-3,3)
dev.off()
## png
## 2
pdf(file="test1.pdf")
curve(dnorm,-3,3)
dev.off()
## png
## 2
data(iris)
print(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
## 7 4.6 3.4 1.4 0.3 setosa
## 8 5.0 3.4 1.5 0.2 setosa
## 9 4.4 2.9 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
## 11 5.4 3.7 1.5 0.2 setosa
## 12 4.8 3.4 1.6 0.2 setosa
## 13 4.8 3.0 1.4 0.1 setosa
## 14 4.3 3.0 1.1 0.1 setosa
## 15 5.8 4.0 1.2 0.2 setosa
## 16 5.7 4.4 1.5 0.4 setosa
## 17 5.4 3.9 1.3 0.4 setosa
## 18 5.1 3.5 1.4 0.3 setosa
## 19 5.7 3.8 1.7 0.3 setosa
## 20 5.1 3.8 1.5 0.3 setosa
## 21 5.4 3.4 1.7 0.2 setosa
## 22 5.1 3.7 1.5 0.4 setosa
## 23 4.6 3.6 1.0 0.2 setosa
## 24 5.1 3.3 1.7 0.5 setosa
## 25 4.8 3.4 1.9 0.2 setosa
## 26 5.0 3.0 1.6 0.2 setosa
## 27 5.0 3.4 1.6 0.4 setosa
## 28 5.2 3.5 1.5 0.2 setosa
## 29 5.2 3.4 1.4 0.2 setosa
## 30 4.7 3.2 1.6 0.2 setosa
## 31 4.8 3.1 1.6 0.2 setosa
## 32 5.4 3.4 1.5 0.4 setosa
## 33 5.2 4.1 1.5 0.1 setosa
## 34 5.5 4.2 1.4 0.2 setosa
## 35 4.9 3.1 1.5 0.2 setosa
## 36 5.0 3.2 1.2 0.2 setosa
## 37 5.5 3.5 1.3 0.2 setosa
## 38 4.9 3.6 1.4 0.1 setosa
## 39 4.4 3.0 1.3 0.2 setosa
## 40 5.1 3.4 1.5 0.2 setosa
## 41 5.0 3.5 1.3 0.3 setosa
## 42 4.5 2.3 1.3 0.3 setosa
## 43 4.4 3.2 1.3 0.2 setosa
## 44 5.0 3.5 1.6 0.6 setosa
## 45 5.1 3.8 1.9 0.4 setosa
## 46 4.8 3.0 1.4 0.3 setosa
## 47 5.1 3.8 1.6 0.2 setosa
## 48 4.6 3.2 1.4 0.2 setosa
## 49 5.3 3.7 1.5 0.2 setosa
## 50 5.0 3.3 1.4 0.2 setosa
## 51 7.0 3.2 4.7 1.4 versicolor
## 52 6.4 3.2 4.5 1.5 versicolor
## 53 6.9 3.1 4.9 1.5 versicolor
## 54 5.5 2.3 4.0 1.3 versicolor
## 55 6.5 2.8 4.6 1.5 versicolor
## 56 5.7 2.8 4.5 1.3 versicolor
## 57 6.3 3.3 4.7 1.6 versicolor
## 58 4.9 2.4 3.3 1.0 versicolor
## 59 6.6 2.9 4.6 1.3 versicolor
## 60 5.2 2.7 3.9 1.4 versicolor
## 61 5.0 2.0 3.5 1.0 versicolor
## 62 5.9 3.0 4.2 1.5 versicolor
## 63 6.0 2.2 4.0 1.0 versicolor
## 64 6.1 2.9 4.7 1.4 versicolor
## 65 5.6 2.9 3.6 1.3 versicolor
## 66 6.7 3.1 4.4 1.4 versicolor
## 67 5.6 3.0 4.5 1.5 versicolor
## 68 5.8 2.7 4.1 1.0 versicolor
## 69 6.2 2.2 4.5 1.5 versicolor
## 70 5.6 2.5 3.9 1.1 versicolor
## 71 5.9 3.2 4.8 1.8 versicolor
## 72 6.1 2.8 4.0 1.3 versicolor
## 73 6.3 2.5 4.9 1.5 versicolor
## 74 6.1 2.8 4.7 1.2 versicolor
## 75 6.4 2.9 4.3 1.3 versicolor
## 76 6.6 3.0 4.4 1.4 versicolor
## 77 6.8 2.8 4.8 1.4 versicolor
## 78 6.7 3.0 5.0 1.7 versicolor
## 79 6.0 2.9 4.5 1.5 versicolor
## 80 5.7 2.6 3.5 1.0 versicolor
## 81 5.5 2.4 3.8 1.1 versicolor
## 82 5.5 2.4 3.7 1.0 versicolor
## 83 5.8 2.7 3.9 1.2 versicolor
## 84 6.0 2.7 5.1 1.6 versicolor
## 85 5.4 3.0 4.5 1.5 versicolor
## 86 6.0 3.4 4.5 1.6 versicolor
## 87 6.7 3.1 4.7 1.5 versicolor
## 88 6.3 2.3 4.4 1.3 versicolor
## 89 5.6 3.0 4.1 1.3 versicolor
## 90 5.5 2.5 4.0 1.3 versicolor
## 91 5.5 2.6 4.4 1.2 versicolor
## 92 6.1 3.0 4.6 1.4 versicolor
## 93 5.8 2.6 4.0 1.2 versicolor
## 94 5.0 2.3 3.3 1.0 versicolor
## 95 5.6 2.7 4.2 1.3 versicolor
## 96 5.7 3.0 4.2 1.2 versicolor
## 97 5.7 2.9 4.2 1.3 versicolor
## 98 6.2 2.9 4.3 1.3 versicolor
## 99 5.1 2.5 3.0 1.1 versicolor
## 100 5.7 2.8 4.1 1.3 versicolor
## 101 6.3 3.3 6.0 2.5 virginica
## 102 5.8 2.7 5.1 1.9 virginica
## 103 7.1 3.0 5.9 2.1 virginica
## 104 6.3 2.9 5.6 1.8 virginica
## 105 6.5 3.0 5.8 2.2 virginica
## 106 7.6 3.0 6.6 2.1 virginica
## 107 4.9 2.5 4.5 1.7 virginica
## 108 7.3 2.9 6.3 1.8 virginica
## 109 6.7 2.5 5.8 1.8 virginica
## 110 7.2 3.6 6.1 2.5 virginica
## 111 6.5 3.2 5.1 2.0 virginica
## 112 6.4 2.7 5.3 1.9 virginica
## 113 6.8 3.0 5.5 2.1 virginica
## 114 5.7 2.5 5.0 2.0 virginica
## 115 5.8 2.8 5.1 2.4 virginica
## 116 6.4 3.2 5.3 2.3 virginica
## 117 6.5 3.0 5.5 1.8 virginica
## 118 7.7 3.8 6.7 2.2 virginica
## 119 7.7 2.6 6.9 2.3 virginica
## 120 6.0 2.2 5.0 1.5 virginica
## 121 6.9 3.2 5.7 2.3 virginica
## 122 5.6 2.8 4.9 2.0 virginica
## 123 7.7 2.8 6.7 2.0 virginica
## 124 6.3 2.7 4.9 1.8 virginica
## 125 6.7 3.3 5.7 2.1 virginica
## 126 7.2 3.2 6.0 1.8 virginica
## 127 6.2 2.8 4.8 1.8 virginica
## 128 6.1 3.0 4.9 1.8 virginica
## 129 6.4 2.8 5.6 2.1 virginica
## 130 7.2 3.0 5.8 1.6 virginica
## 131 7.4 2.8 6.1 1.9 virginica
## 132 7.9 3.8 6.4 2.0 virginica
## 133 6.4 2.8 5.6 2.2 virginica
## 134 6.3 2.8 5.1 1.5 virginica
## 135 6.1 2.6 5.6 1.4 virginica
## 136 7.7 3.0 6.1 2.3 virginica
## 137 6.3 3.4 5.6 2.4 virginica
## 138 6.4 3.1 5.5 1.8 virginica
## 139 6.0 3.0 4.8 1.8 virginica
## 140 6.9 3.1 5.4 2.1 virginica
## 141 6.7 3.1 5.6 2.4 virginica
## 142 6.9 3.1 5.1 2.3 virginica
## 143 5.8 2.7 5.1 1.9 virginica
## 144 6.8 3.2 5.9 2.3 virginica
## 145 6.7 3.3 5.7 2.5 virginica
## 146 6.7 3.0 5.2 2.3 virginica
## 147 6.3 2.5 5.0 1.9 virginica
## 148 6.5 3.0 5.2 2.0 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 150 5.9 3.0 5.1 1.8 virginica
#Part A
"Histogram of Sepal Length"=hist(iris$Sepal.Length, main = "Histogram of Sepal Length", xlab = "Sepal Length", ylab = "Frequency")
#Part B
species_counts=table(iris$Species)
"Bar Plot of Species"=barplot(species_counts, main = "Bar Plot of Species", xlab = "Species", ylab = "Frequency")
#Part C
"Pie Chart of Species"=pie(species_counts, main = "Pie Chart of Species", col = c("red", "green", "blue"), labels = c("setosa", "versicolor", "virginica"))
#Part D
"Stem and Leaf plot of Sepal Length"=stem(iris$Sepal.Length)
##
## The decimal point is 1 digit(s) to the left of the |
##
## 42 | 0
## 44 | 0000
## 46 | 000000
## 48 | 00000000000
## 50 | 0000000000000000000
## 52 | 00000
## 54 | 0000000000000
## 56 | 00000000000000
## 58 | 0000000000
## 60 | 000000000000
## 62 | 0000000000000
## 64 | 000000000000
## 66 | 0000000000
## 68 | 0000000
## 70 | 00
## 72 | 0000
## 74 | 0
## 76 | 00000
## 78 | 0
#Part E
"Boxplot of Sepal Length"=boxplot(iris$Sepal.Length, main = "Boxplot of Sepal Length", ylab="Sepal Length")
#Part F
"Box and Whisker plot of Sepal Length by Species"=boxplot(Sepal.Length ~ Species, data = iris, main = "Boxplot of Sepal Length by Species", xlab = "Species", ylab = "Sepal Length")
#Part G
"Scatter Diagram of Sepal Width vs. Sepal Length"=plot(iris$Sepal.Width, iris$Sepal.Length, main = "Scater Plot of Sepal Width vs. Sepal Length", xlab = "Sepal Width", ylab = "Sepal Length")
#Part H
"Scatter Plot of Sepal Width vs. Sepal Length by Species"=plot(iris$Sepal.Width, iris$Sepal.Length, main = "Scatter Plot of Sepal Width vs Sepal Length by Species", xlab = "Sepal Width", ylab = "Sepal Length", col = as.numeric(iris$Species), pch = c(1, 2, 3)[as.numeric(iris$Species)])
legend("topright", legend = levels(iris$Species), col = 1:3, pch = c(1, 2, 3))
"Petal Width vs Petal Length"=plot(iris$Sepal.Width, iris$Petal.Length, main = "Scatter Plot of Petal Width vs Petal Length", xlab = "Petal Width", ylab = "Petal Length")
model=lm(Petal.Length ~ Petal.Width, data = iris)
abline(model, col = "red")
##Question 3
#Part A
data=rnorm(1000, mean = 0, sd = sqrt(3))
#Part B
"Histogram with Density Plot"=hist(data, prob=TRUE, main ="Histogram with Density Plot", xlab="Value", ylab= "Density")
lines(density(data), col= "blue")
#Part C
"Normal Q-Q Plot"=qqnorm(data, main = "Normal Q-Q Plot")
qqline(data, col = "red")
#Part D
First50=data[1:50];First50
## [1] 0.7808466 2.6966513 -1.0957732 -1.4388859 3.2691153 -0.3704754
## [7] 1.3313255 -2.4992745 1.6657722 0.9958959 -2.0149103 0.2059128
## [13] 0.1986843 -0.8852863 0.0628782 -0.2363038 1.5397009 -0.3495647
## [19] 0.3627698 1.7993692 0.2450666 1.7146874 0.3020525 2.1587916
## [25] -4.9508280 -2.4604901 2.6252085 1.5912215 -0.3011414 -0.8427012
## [31] -2.0731301 1.5634418 -0.1606179 1.3511993 -0.5109466 2.5325405
## [37] 0.1336005 1.9446681 -0.5377345 -1.1393394 3.0929398 -1.1499438
## [43] -4.0436497 -0.4298569 -0.1528365 -2.4034746 -3.0825900 0.2272097
## [49] 1.0301019 -1.3946412
#Part E
std_dev=sd(data)
mean_data=mean(data)
within_two_sd <- data[data > (mean_data - 2 * std_dev) & data < (mean_data + 2 * std_dev)]
percentage_within_two_sd <- (length(within_two_sd) / length(data)) * 100
print(percentage_within_two_sd)
## [1] 94.8