- Create a for loop that finds the sum of the following vector (without using the sum() function)
myvector <- c(1,2,3,4,5,6)
- Modify this code to find the mean of the vector (don’t use mean())
- Modify your code to create two functions, one called “SumMyVector” and the other “MeanMyVector”
SumMyVector(a)
[1] 21
MeanMyVector(a)
[1] 3.5
2.
- Create a loop that will take a vector of numeric grades and create a corresponding vector containing letter grades. Base it on the following rules;
100 >= A >=90, 89 >= B >= 80, 79 >= C >= 70, 69 >= D >= 60, 59 >= F
It should do something like this.
#input
NumbericGrade = c(95, 79, 92, 70)
#Your code will produce the following vector
c("A", "C", "A", "D")
Use it to grade the following vector.
grades <- c(98,49,67,23,59,78,82)
- Modify your loop to be a function called “Grader” that takes a vector of numeric grades as its argument
3.
- Create a vector x containing a sequence 1 to 100 by 1
- Create a vector y equal to 2*x
- Create a plot of y as a function of x
- Use the rnorm() function to add “noise” to y. Set standard deviation to 15
- Creat a plot of y as a function of x

4.
Write a function called pairwise.max that takes as its arguments two vectors x and y and returns a vector whose 1st element is the larger of the 1st elements of x and y, whose 2nd element is the larger of the 2nd elements of x and y, etc.
#imagine you have these two vectors
x <- c(1,4,2,6,3,4,5)
y <- c(5,3,1,8,7,6,1)
# your code sould produce the following vector
z <- c(5,4,2,8,7,6,5)
5.
- Create a for loop that runs 100 times and grows a vector where the elements of this vector are means of 100 random uniform numbers. e.i., use mean(runif(100)).
- Your vector will essentially be this, but use a for loop to generate it;
c(mean(runif(100)), mean(runif(100)), mean(runif(100)), ……, mean(runif(100)))
- Use the hist() function to create a histogram of the vector you grew
- Rerun this code but run the loop 1000 times. Creat another histogram and comment on the difference between the two plots.
- Create a function that reruns this code including the plot. The argument of this funtion should be how many times you would like the loops to run.
BONUS- Give the name of this phenomenon