a <- c(1, 2, 3, 4, 5)
print(a)[1] 1 2 3 4 5
typeof(a)[1] "double"
Exercise 1
Logical, integer, double, and character
A matrix can only contain numeric values whereas a data frame can contain other values, such as characters.
It coerces logical values into integer values.
It will create a character matrix.
Exercise 2
a <- c(1, 2, 3, 4, 5)
print(a)[1] 1 2 3 4 5
typeof(a)[1] "double"
b <- 1:5
print(b)[1] 1 2 3 4 5
typeof(b)[1] "integer"
c <- c(sqrt(2), 4.7e4, 1/0)
print(c)[1] 1.414214 47000.000000 Inf
typeof(c)[1] "double"
d <- c(T, T, T, T)
print(d)[1] TRUE TRUE TRUE TRUE
typeof(d)[1] "logical"
e <- c("1", 2, 3)
print(e)[1] "1" "2" "3"
typeof(e)[1] "character"
f <- c(7L, NA, NA, 5L, 3L)
print(f)[1] 7 NA NA 5 3
typeof(f)[1] "integer"
g <- c(7L, "NA", "NA", 5L, 3L)
print(g)[1] "7" "NA" "NA" "5" "3"
typeof(g)[1] "character"
h <- c()
print(h)NULL
typeof(h)[1] "NULL"
Exercise 3
head(airquality) Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
a
Ozone1 <- airquality$Ozoneb
Ozone2 <- is.na(Ozone1)
table(Ozone2)Ozone2
FALSE TRUE
116 37
There are 37 NA values in the dataset.
c
Ozone3 <- as.integer(Ozone2)
typeof(Ozone3)[1] "integer"
summary(Ozone3) Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0000 0.0000 0.0000 0.2418 0.0000 1.0000
summary(airquality$Ozone) Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
1.00 18.00 31.50 42.13 63.25 168.00 37
sd1 <- sd(airquality$Ozone)
sd1[1] NA
sd2 <- sd(airquality$Ozone, na.rm = TRUE)
sd2[1] 32.98788
Summary of air quality Ozone returns the five number summary for the variable Ozone from the original dataset, without NA values removed but it accounts for the na values and removes them automatically.
sd of airqality Ozone returns the standard deviation for the variable Ozone from the original dataset without NA values removed. Thus an NA is created by an error.
sd(airqualityOzone, na.rm = TRUE) removes the NAs, allowing it to calculate the standard deviation. As shown by the code below
var1 <- sd(airquality$Ozone, na.rm = TRUE)
var2 <- na.omit(airquality$Ozone)
print(var1)[1] 32.98788
sd(var2)[1] 32.98788
e
var3 <- na.omit(airquality)
var4 <- nrow(airquality) - nrow(var3)
var4[1] 42
There are 42 NA values in the dataset. As show by the code above.
Exercise 4)
a
p1 <- seq(0, 1, (0.2))b
fofp1 <- (p1)*(1-p1)
var5 <- plot(p1,fofp1,type='b',ylab='f(p1) = p1*(1-p1)', col="RED", main ="Graph 1")var5NULL
c
p2 <- seq(0, 1, (0.01))fofp2 <- (p2)*(1-p2)
var6 <- plot(p2,fofp2,type='o',ylab='f(p2) = p2*(1-p2)', col= "BLUE", main = "Graph 2")var6NULL
Exercise 5)
movies <- read.csv("https://ericwfox.github.io/data/movies.csv")hist(movies$runtime, main="Histogram of Runtime", xlab="Runtime", col="PINK")By the histogram above it appears to be skewed left, with mean roughtly centered around 100 minutes.
There are several potential outliers as shown by the boxplot below, which is also skewed.
boxplot(movies$runtime, main="Boxplot of Runtime", xlab="Runtime", col="LIGHTBLUE")b
lmmodel <- lm(movies$critics_score~movies$runtime)
plot(movies$critics_score, movies$runtime)
abline(lmmodel, col="BLUE", lwd=4)There does not appear to be a linear association between movie critic scores and movie runtimes.
movies$mpaa_rating <- factor(movies$mpaa_rating , levels=c("G", "PG", "PG-13", "R", "NC-17", "Unrated"))
boxplot(movies$imdb_rating ~ movies$mpaa_rating, xlab="MPAA Rating", ylab="IMDB Rating", col='RED')