Data Type and Data Classes: Exercises

Author

Termeh Shafie

Exercise

Double check that you do not have stored objects in your current session with the following command. This will list all objects that you have in your current R session.

ls()
character(0)

In case you have objects that you want to remove from the current session you can do so with the rm() function. This command will remove all objects available in your current environment.

rm(list = ls())

This command uses commands that we have not talked about yet. If you do not understand how it works now, you will do so after tomorrows lectures and exercises.

  1. Create variables var1 and var2 and initialize them with two integers of choice.

  2. Add the two variables and save them as a new variable named var3 and print the result.

  3. Check the class, mode, and type for var1, var2, var3 and π (is found under the variable name pi in R)

  4. Create two character variables containing a text of choice. Check the mode, class, and type of the first one.

Add var1 to it. What is the result and why?

  1. Convert var3 to an integer, cast an integer variable to double, cast a string to a double.

  2. Report floor and ceiling of π and round π to 3 decimal places.

  3. Is floor of π an integer?

  4. Treat "3.56437" string as number.

  5. Divide ∞ by - ∞

  6. Print a truth table for OR (for three distinct logical values). Read about truth tables here.

  7. Multiply a logical TRUE by a logical FALSE. Rise the logical true to the 7-th power.

  8. Create two character variables containing two verses of a chosen song.

  • Concatenate the two variables,
  • Paste the variables with ‘*’ as separator.
  • Find if ‘and’ occurs in the second line,
  • Substitute a word for another,
  • Extract substring starting at the 5th character and 5 characters long.