The following is for midterm 1. Please let me know if anything else is needed!
#1
P1<-c(23,45,67,46,57,23,83,59,12,64)
P1
## [1] 23 45 67 46 57 23 83 59 12 64
#a)
max(P1)
## [1] 83
#b)
min(P1)
## [1] 12
#c)
mean(P1)
## [1] 47.9
# Note we can also use
summary(P1)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 12.00 28.50 51.50 47.90 62.75 83.00
#2
P1[9]<- 42
P1
## [1] 23 45 67 46 57 23 83 59 42 64
#a)
summary(P1)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 23.00 42.75 51.50 50.90 62.75 83.00
# In this case, we can see that the P1 in question 1 is 47.90 vs. Question 2 at 50.90
#b)
length(P1[P1>40])
## [1] 8
# Here we can see that there are 8 values in P that are greater than 40!
#c)
mean(P1[P1>40])
## [1] 57.875
# The mean is of the values greater than 40 is 57.85
#3 (I used P1 as a reference in question 1, but the idea can be extended past array/vectors)
#a) x <- 1==1
# This returns true.
#b) x <- 1==0
#This returns false.
#c) x <- ! 1==1
#this returns false ( the ! is telling us not,or negating the first one)
#d) x <- if (1==0) 0 else 1
#this returns 1.
#e) x <- if (10) 1 else 0
#This returns 1
#f) x <- if (1==0) 0
#This returns null given that there is no "else". This means that we NEED 1==0, but would not be in the case.
#4
smallest_number= function(x){
if (length(x)==5){return(min(x))}
else{"Too few or many inputs"}
}
smallest_number (c(1,2,3,4,5))
## [1] 1
#5
histo<-rnorm(100)
hist(histo,probability=TRUE,col="red")