Complete the following using R. Copy and paste your results from the console into a document to submit:
a <- 5
b <- 7
a+b
## [1] 12
b/a
## [1] 1.4
7/5
## [1] 1.4
12%/%5
## [1] 2
18%%5
## [1] 3
a==5
## [1] TRUE
a<5
## [1] FALSE
a>5
## [1] FALSE
y = curve (3*x^2-2*x +15, from = -1, to = 2, n=10)
none <- function(x) {
y <- x
return(y)
}
none(3)
## [1] 3
mulp <- function(x,y) {
z= x*y
return(z)
}
mulp(4,8)
## [1] 32
a<-5; b<-7
mulp(a,b)
## [1] 35
a=5; b=7
mulp(none(6),mulp(5,7) )
## [1] 210
4.Access information on the rep function using help(rep) or ?rep, then use the rep function in R to create the following sequences:
# help(rep)
x <- c(2,6,10,14,18)
rep(x ,each=3 )
## [1] 2 2 2 6 6 6 10 10 10 14 14 14 18 18 18
#OR
y <- seq(2,18,by=4)
rep(y ,each=3 )
## [1] 2 2 2 6 6 6 10 10 10 14 14 14 18 18 18
rep(y ,times=3 )
## [1] 2 6 10 14 18 2 6 10 14 18 2 6 10 14 18
rep(y ,each=3 , len=17)
## [1] 2 2 2 6 6 6 10 10 10 14 14 14 18 18 18 2 2
5.Set v equal to the vector in 4(a), and complete the following: a. Use the sum function to find the sum of the values. b. Use help to find the mean function, then find the mean of the values in the vector. c. Calculate the square root of each value in v.
v= rep(y ,each=3 )
sum(v)
## [1] 150
mean(v)
## [1] 10
sqrt(v)
## [1] 1.414214 1.414214 1.414214 2.449490 2.449490 2.449490 3.162278 3.162278
## [9] 3.162278 3.741657 3.741657 3.741657 4.242641 4.242641 4.242641
6.Create a vector x which contains the values between 10 and 20 in increments of 0.1 a. Create a vector y which contains the logarithm of the values in the vector x. b. Plot the values of x versus y. Label the axes in the plot.
x<- seq(10,20,0.1)
y<-log(x, base=10)
plot(x,y)
y[30]
## [1] 1.11059
y[30:35]
## [1] 1.110590 1.113943 1.117271 1.120574 1.123852 1.127105
y[c(50,60)]
## [1] 1.173186 1.201397
percentages <- c(72,81,52,63)
Counties <- c('A', 'B', 'C', 'D')
names(Counties) <- percentages
Counties
## 72 81 52 63
## "A" "B" "C" "D"
County <- c("County A"= 72, "County B"=81, "County C"=52, "County D"=63)
barplot(County,
col =100,
xlab = "Votes",
ylab = "Counties",
main = "bans smoking in public places")
mean(percentages)
## [1] 67
sd(percentages)
## [1] 12.40967
8.Consider the following data set with responses to the questions (1) What is your favorite hot drink? (2) What is your favorite cookie? {coffee, tea, cocoa, tea, tea, chai, coffee, cocoa, coffee, coffee, coffee, chai, tea, tea} {chocolate chip, peanut butter, chocolate chip, oatmeal, oatmeal, shortbread, chocolate chip, sandwich cookie, oatmeal, oatmeal, chocolate chip, sandwich cookie, peanut butter, shortbread}
Do not type this data in full. Instead, give each drink and each biscuit a number and make vectors of the numbers corresponding to the lists above (just type the numbers without quotes).
Turn your vectors into factors with the right names and tabulate the levels.
drinks <- c(1,2,3,2,2,4,1,3,1,1,1,4,2,2)
cookies<- c(1,2,1,3,3,4,1,5,3,3,1,5,2,4)
f.drinks <- factor(drinks) # Factore is used for categorical data
levels(f.drinks)[1] <- "coffee"
levels(f.drinks)[2] <- "tea"
levels(f.drinks)[3] <- "cocoa"
levels(f.drinks)[4] <- "chai"
f.cookies <- factor(cookies)
levels(f.cookies)[1] <- "chocolate chip"
levels(f.cookies)[2] <- "peanut butter"
levels(f.cookies)[3] <- "oatmeal"
levels(f.cookies)[4] <- "shortbread"
levels(f.cookies)[5] <- "sandwich cookie"
table(f.drinks,f.cookies)
## f.cookies
## f.drinks chocolate chip peanut butter oatmeal shortbread sandwich cookie
## coffee 3 0 2 0 0
## tea 0 2 2 1 0
## cocoa 1 0 0 0 1
## chai 0 0 0 1 1
M <- matrix(1:9,ncol=3,nrow = 3, byrow = T)
rownames(M)<- c("C", "O", "L")
colnames(M)<- c("R", "O", "W")
M
## R O W
## C 1 2 3
## O 4 5 6
## L 7 8 9
M[2,2]
## [1] 5
M[2,]
## R O W
## 4 5 6
M[,3]
## C O L
## 3 6 9
M %*% t(M)
## C O L
## C 14 32 50
## O 32 77 122
## L 50 122 194
det(M)
## [1] 6.661338e-16
diag(M)
## [1] 1 5 9
N <- M * 1/3
N+M
## R O W
## C 1.333333 2.666667 4
## O 5.333333 6.666667 8
## L 9.333333 10.666667 12
round(N, 2)
## R O W
## C 0.33 0.67 1
## O 1.33 1.67 2
## L 2.33 2.67 3
floor(N+M)
## R O W
## C 1 2 4
## O 5 6 8
## L 9 10 12
NN <- matrix(1:1,nrow=3,ncol=3)
N %*% NN
## [,1] [,2] [,3]
## C 2 2 2
## O 5 5 5
## L 8 8 8
col1 <- seq(1,20,1)
col2 <- rep(seq(1,4), each=5)
col3 <- rbinom(20,30,0.4)
M <- cbind(col1,col2,col3)
class(M)
## [1] "matrix" "array"
x <- data.frame(col1,col2,col3)
class(x)
## [1] "data.frame"
x[[2]][3]
## [1] 1
x[[2]]
## [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
x[3,]
## col1 col2 col3
## 3 3 1 10
names(x) <- c("trial", "treatment", "result")
plot(airquality)
colnames(airquality)
## [1] "Ozone" "Solar.R" "Wind" "Temp" "Month" "Day"
plot (airquality$Ozone, airquality$Temp, xlab = "Ozone",
ylab = "Temperature", main = "Ozone vs.Temperature", pch=19)
plot (airquality$Wind, airquality$Temp, xlab = "Wind",
ylab = "Temperature", main = "Wind vs.Temperature", pch=6)
par(mfrow=c(1,2))
plot (airquality$Ozone, airquality$Temp, xlab = "Ozone",
ylab = "Temperature", main = "Ozone vs.Temperature", pch=19)
plot (airquality$Wind, airquality$Temp, xlab = "Wind",
ylab = "Temperature", main = "Wind vs.Temperature", pch=6)
par(mfrow=c(1,2))
boxplot(airquality$Temp ~ airquality$Month,
main= "Temp in Months",
xlab = "Months",
ylab= "Temp Observations")