Exercises for 1.2: Variables & the R workspace

Exercise 1.2.1: Introduction to variables

  • Create a variable called potato whose 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 potato by typing the variable name.
  • Do it again using the print() function
  • Calculate the square root of potato using the sqrt() function.
  • Print out the value of potato again to verify that the value of potato hasn’t changed
  • Reassign the value of potato to 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 age whose 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 age greater 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 airquality data 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 airquality at 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 Wind variable in airquality
  • Print out the third element of the Wind variable

Exercise 1.2.6: Working with data frames

  • Create a new data frame called aq that 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:10 as a shorthand method!
  • Use logical indexing to print out all days (ie. cases) in aq where the Ozone level was higher than 20. (Note how the output deals with the NA values)
  • Use subset() to do the same thing. Notice the difference in the output.
  • Create a TooWindy variable inside aq, which is a logical variable that is TRUE if Windy is greater than 10, and FALSE otherwise
  • 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!