Giving Names to Data

To store a data object, you give it a name. This is called assignment: we assign data values a name. (See help("assignOps").)

This may be written several ways. The two most common are the “left arrow”, <-, and the single equals =. The left arrow is generally preferred, because it is unambiguous. But the equals symbol is often used by people coming to R from other programming languages, so it’s use is fairly common.

x <- 7
y = 8

x + y
[1] 15

Assigning a name to data stores the data in your computer’s memory. You can list the objects available in your current session with

ls()
[1] "x" "y"

Good Names

There are a lot of naming conventions and advice to be found on the internet. My free advice:

  • Use a consistent naming scheme. This will help you and anyone else who might need to read your code in the future.
  • Meaningful names are helpful. For instance, “age” rather than “x”.
  • Short names are easier to read and type. Long names make your code harder to scan. There is a tension between having meaningful names and having short names.

  • Capitalization matters.
  • Begin a name with a character.
  • Keep names compact, no spaces.
  • Avoid most special characters (!@#$%^&*). The main exception is using underscores,“_“.

Removing Data

It is a good idea to clean up your workspace as you go, removing data objects that are no longer needed. This makes it easier to keep track of the key data objects you want to work with.

To clean up your workspace, use

remove(x)

ls()
[1] "y"

A common alias for remove() is rm(). You can use these functions to remove several objects at once, see help("remove").

Reusing Names

When you reuse an object name for assignment, you are throwing out the old data. This is considered a routine action, so there is no warning or error.

y
[1] 8

y <- c("red", "green", "blue")
y
[1] "red"   "green" "blue" 

Exercises

  1. Assign the value 3 the name “v”, and the value 2 the name “w”. Then calculate \(v + w\).

  2. Bad Names - assign the value 7 the name “one”, and the value 2 the name “three”. Calculate \(one^{three}\). Never bring me code that looks like this!

  3. Capitalization - assign 8 to “a” and 0 to “A”. Calculate the mean of “a” and “A”.

    Here, “a” and “A” look different enough that this might be acceptable. However, “x” and “X” look similar enough that they would be a poor choice of names here.

  4. Try assigning a data value to the name “1$” (without the quotes). Try the name “1a” (again, no quotes). It is interesting that this gives two different error messages - any idea why?

    Remarkably, R allows you to use names like these. However, such non-syntactic names require you to use backticks (back quotes). In principle you could use unicode symbols as names, like \(\mu\) or \(\sigma\). However, current R code editing software does not make this easy - maybe in the future?

  5. Tidy up by removing all the data objects from your workspace.