5.1 Scalars

The simplest object type in R is a scalar. A scalar object is just a single value like a number or a name. In the previous chapter we defined several scalar objects. Here are examples of numeric scalars:

# Examples of numeric scalers
a <- 100
b <- 3 / 100
c <- (a + b) / b

Scalars don’t have to be numeric, they can also be characters (also known as strings). In R, you denote characters using quotation marks. Here are examples of character scalars:

# Examples of character scalers
d <- "ship"
e <- "cannon"
f <- "Do any modern armies still use cannons?"

As you can imagine, R treats numeric and character scalars differently. For example, while you can do basic arithmetic operations on numeric scalars – they won’t work on character scalars. If you try to perform numeric operations (like addition) on character scalars, you’ll get an error like this one:

a <- "1"
b <- "2"
a + b
Error in a + b: non-numeric argument to binary operator

If you see an error like this one, it means that you’re trying to apply numeric operations to character objects. That’s just sick and wrong.