Object Naming and Other Style Concerns

Learning Goals

At the end of this lesson, you should:

  • understand how to assign variables and collections of numbers to an object name
  • know the rules for how to name objects
  • understand reserved words in R and how to find them
  • understand how white space functions in R

Object assignment

It is rather cumbersome to continually retype or paste information. We can avoid this by assigning information to an R object.

Traditionally in R, the left arrow is used for object assignment, <- (the less-than symbol and a dash), but the standard equals sign, = also works.

These are equivalent:

x <- 1
x
[1] 1
x = 1
x
[1] 1

We can assign multiple numbers to an object:

x_vector <- 1:10
y_vector <- c(2, 4, 6, 8, 10)

The left arrow assignment <- takes everything on the right side of the arrow and assigns it the object name on the left.

Object naming

It is your choice (mostly) about what to name R objects. There are a few rules and conventions to follow:

  • Choose a name that is short, yet descriptive.
  • Spaces are generally not allowed and a huge pain - so avoid doing this.
  • Don’t start an object name with a number or symbol (this is technically possible, but also a pain).
  • R is case sensitive, so test is different from Test and TEST. Be mindful of this! It trips many folks up.
  • It is possible that future you will thank your past self for using lowercase and avoiding special symbols (aside from . and _)
  • If you start a function name with a “.” (e.g. .variable), you won’t see it listed in the global environment (which can be frustrating). This is not recommended for newer R users.
  • You cannot use “reserved words” from the R language (terms set aside for very specific purposes in R). When typing these in an R console, they usually light up in a special colors.

Here is some discussion on object naming in R.

Reserved words

reserved word meaning
TRUE FALSE logical
NA missing value
NaN not a number/undefined
NULL no value/undefined
Inf -Inf infinity
for in for loops
if else while break next repeat control flow
NA_integer_ NA_real_ NA_complex_ NA_character_ missing data by data type

It’s easy to forgot these. Run ?reserved in an R console or check here to remind yourself if need be.

Some examples of reserved words in the wild:

log(0)
[1] -Inf
0/0
[1] NaN
2/0
[1] Inf

Some additional notes on R syntax

  • most often, the amount of white space does not matter.

These are the same:

4/3
[1] 1.333333
4/    3
[1] 1.333333
4    /  3
[1] 1.333333

These are also the same:

log(10)
[1] 2.302585
log( 10 )
[1] 2.302585
log ( 10)
[1] 2.302585
  • R expects certain things to be paired or completed before it will send it to the interpreter

  • As mentioned, earlier a hard return is sufficient to send a command to the R interpreter.

  • Exceptions: binary operators (= those expecting 2 numbers): +, -, *, /, ^, ==, etc. R is waiting for these to be ‘completed’.

  • Exceptions: unclosed parentheses (), brackets [] {}, or quotes ' ' " ". R will wait for these to be completed. A single quote must always be complemented by a second single quote, and a double quote likewise must always have a second quote to complete it. Left parentheses, curly braces, or brackets much also be accompanying by their right-sided complement.

  • Examples

1 + 2
{ }
( )
[ ]
"  "
' '
` `
  • Errors
1 + 
'
(  } ] 
"
' "
  • There is no difference between double and single quotes on a practical level, but R will interpret them as different commands (so a single quote cannot close a double quote). This is useful when there is nested levels of quoting. This is uncommon, but, it does happens now and then.

Example:

"r `format(Sys.Date(), '%b %d, %Y')`"
[1] "r `format(Sys.Date(), '%b %d, %Y')`"

If this particular piece of code makes no sense to you, do not worry. The point of presenting this code at this stage in your journey of learning R is to demonstrate how single quotes, double quotes and the backtick ` can be used together in single statement.

Putting it all together

Matching parentheses, quotes, and other paired structures is important in R. The R interpreter may stop if it is waiting for a statement to be ‘closed’. As a result, RStudio will often automatically append a pair while you type. Try typing a single quote, double quote, square bracket, curly bracket, parentheses or backtick and notice how this happens.

Rstudio furthers this practice when you highlight text. In RStudio, highlight some text and then type the key for double quotes. What happened? Try the same with parentheses and the other keys/symbols mentioned. Once you get used to this, it will save you some time!