Exercises for 1.2: Variables & the R workspace

Exercise 1.2.1: Introduction to variables

  • Create a variable called potatowhose value corresponds to the number of potatos you’ve eaten in the last week. Or something equally stupid. It doesn’t matter.
  • Print out the value of potatoby typing the variable name.
  • Do it again using the print()function
  • Calculate the square root of potatousing the sqrt()function.
  • Print out the value of potato again to verify that the value of potatohasn’t changed
  • Reassign the value of potatoto potato * 2
  • Print out the new value of potato to verify that it has changed

Exercise 1.2.2: Variables of different types

  • you’ve already created a numeric variable: now try making a character (string) variable and a logical variable
  • try creating (numeric) variables with “special” values: Inf(infinity), -Inf(minus infinity), NaN(“not a number”)
  • try creating a variable with a “missing” value NA
  • try creating a variable with a “non-existant” value NULL

Exercise 1.2.3: Creating vectors

  • Create a numeric vector with three elements using c()
  • Create a character vector with three elements using c()
  • Create a numeric vector called agewhose elements contain the ages of three people you know, where the names of each element correspond to the names of those people

Exercise 1.2.4: Indexing vectors

  • use “indexing by number” to get R to print out the first element of one of the vectors you created in Exercise 1.2.3.
  • use negative indices to get R to print out everything except the first element of that vector
  • use logical indexing to return all the ages of all people in agegreater than (say) 25 (or some other number if that makes the results more interesting)
  • use indexing by name to return the age of one of the people whose ages you’ve stored in age

Exercise 1.2.5: Variables inside data frames

For this exercise, we’ll use one of the data frames that comes bundled with R, rather than trying to create a new one. The airqualitydata frame contains 153 cases and 6 variables. You can’t actually see it in the workspace because R is storing it in a “hidden” location (sort of).

  • Type airqualityat the command line to see what it looks like. (I won’t include the output for this in the solution set because it’s 153 lines long!)
  • Use the $method to print out the Windvariable in airquality
  • Print out the third element of the Windvariable

Exercise 1.2.6: Working with data frames

  • Create a new data frame called aqthat includes only the first 10 cases. Hint: typing c(1,2,3,4,5,6,7,8,9,10)is tedious. R allows you to use 1:10as a shorthand method!
  • Use logical indexing to print out all days (ie. cases) in aqwhere the Ozone level was higher than 20. (Note how the output deals with the NAvalues)
  • Use subset()to do the same thing. Notice the difference in the output.
  • Create a TooWindyvariable inside aq, which is a logical variable that is TRUEif Windyis greater than 10, and FALSEotherwise
  • Delete that variable

Exercise 1.2.7: Creating factors

  • Create a factor corresponding to a categorical variable that can take on three levels: "male","female","other"

Exercise 1.2.8: Removing variables

  • clear the workspace using Rstudio’s “clear all” button
  • create a variable and then remove it using rm().
  • try to print out the value of the removed variable just to see what happens!