Class Exercises

  1. Use the help function to learn about the “colon operator”. Type help(“:”). If you’re using RStudio, you could also type : in the Help pane search box.
help(":")
  1. Using what you learned from the help page for the colon operator, create a variable called first10 with the numbers 1 through 10 in it.
first10 <- 1:10
  1. Now try multiplying firstTen by 10 (first10 * 10). What was the result?
first10*10
##  [1]  10  20  30  40  50  60  70  80  90 100
  1. Using the colon operator and the sum function, find out what the sum of the integers from 1 to 1000 is.
sum(1:1000)
## [1] 500500
  1. Create a factor vector called myFavoritePies and put the names of your favorite pies in it (if you have no favorite pies, reconsider your life priorities).
myFavoritePies <- factor(c('Pumpkin','Pecan'))
  1. Change myPies so that it contains your favorite pies AND tells R about some of the other kinds of pies that exist but are not your favorite.
myPies <- factor(c('Pumpkin','Pecan','Chocolate','Banana Cream','Mincemeat'))
  1. Use the ls function to see the variables you have created. You can also view the variables you have created in the “environment” tab in the upper right hand panel of RStudio.
ls()
## [1] "first10"        "myFavoritePies" "myPies"
  1. Use the history function to view the commands you have typed (or just click on the “History” tab in the upper right panel of RStudio). Double click on the line you used to calculate the answer to question 4. Now modify the command to find the sum of the EVEN numbers from 1 to 1000.
#history() # commented out to save space here. Just remove the first # to run
sum(seq(2, 1000, by=2))
## [1] 250500