What is a type?

Programming languages organize different kinds of data into “types.” Programming languages do this in order to provide structure and logic to programs. There are three basic types in R, though other types exist too.

Numeric

Numbers in R have the numeric type. Numbers work the way you would expect them to, in other words you can use them with all of the arithmetic operators: +, -, *, /, and also with operators for exponentiation ^ and modulus %%. Numbers also work with logical operators which will be explained in the next section.

# Addition
3 + 2
## [1] 5
# Subtraction
3 - 2
## [1] 1
# Multiplication
3 * 2
## [1] 6
# Division
3 / 2
## [1] 1.5
# Exponentiation
3^2
## [1] 9
# Scientific Notation
10e2
## [1] 1000
# Modulus
10 %% 3
## [1] 1

Logical

Three values in R have the logical type: TRUE, FALSE, and NA. NA represents missing data in R while TRUE and FALSE represent the value of a logical expression. Expressions that evaluate to logical values often involve the logical operators. The following logical operators are usually used to compare numbers:

  • greater than >
  • greater than or equal >=
  • less than <
  • less than or equal <=
  • exactly equal ==
  • not equal !=

Here are some examples using these operators and the resulting logical values:

3 > 10
## [1] FALSE
9 >= 8
## [1] TRUE
-3 < 6
## [1] TRUE
4 <= 23
## [1] TRUE
5 >= 5
## [1] TRUE
0.003 == 0.003
## [1] TRUE
28 != 43
## [1] TRUE

There are other logical operators that are used for relating logical values:

  • and &&
  • or ||
  • not !

Here are some examples using these operators and the resulting logical values:

TRUE && TRUE
## [1] TRUE
FALSE && TRUE
## [1] FALSE
TRUE && FALSE
## [1] FALSE
FALSE && FALSE
## [1] FALSE
TRUE || TRUE
## [1] TRUE
FALSE || TRUE
## [1] TRUE
TRUE || FALSE
## [1] TRUE
FALSE || FALSE
## [1] FALSE
!TRUE
## [1] FALSE
!FALSE
## [1] TRUE

Logical values can also be used with == and != operators.

TRUE == TRUE
## [1] TRUE
TRUE == FALSE
## [1] FALSE
TRUE != FALSE
## [1] TRUE

All of these logical operators can be combined in expressions. Here is the order of operations in terms of logical operators, which all come after any mathematical operations:

  1. >, >=, <, <=, ==, !=, !
  2. &&
  3. ||

Here are examples:

3 > 4 && 5 <= 10
## [1] FALSE
4 != 9 || 6 > 0
## [1] TRUE
6 + 9 >= 15 && 8 * 6 > 3
## [1] TRUE
5 + 1 > 6 && 4 != 5 || 9 * 7 < 1000 && TRUE != FALSE
## [1] TRUE

Exercises

Should the following expressions evaluate to TRUE or FALSE? Try to work it out on paper before you check your answer in the R console.

  1. 3 > 9 || 8 >= 2^3
  2. 5 > 1 && 3 <= 3 && 4 + 9 * 2 <= 5^3 || (4^2 * 3 > 8 * 5 + 2 || 7^3 > 350) && 9^4 <= 5^6
  3. [Any R expression that evaluates to a logical value] || TRUE

Character

The character type can be thought of as a piece of text, or as a programmer might call it a string. Strings are text surrounded by single or double quotes.

"I am a string!"
## [1] "I am a string!"
'I am also a string!'
## [1] "I am also a string!"

There’s no difference between a string that is single or double quoted as long as it contains the same text. We can test this with the logical operator for equality (==).

"Are we the same?" == 'Are we the same?'
## [1] TRUE

The logical operators for equality test whether the contents of the string are equal. For example:

"Are we the same?" == "No way."
## [1] FALSE
"I'm unique!" != "We're different!"
## [1] TRUE

Type Coercion

If you try to use the arithmetic and logical operators we’ve used with different types R will attempt to coerce the operator’s arguments so that the operator can still work without producing an error. Here are examples of adding a logical value to a numeric value:

0 + TRUE
## [1] 1
0 + FALSE
## [1] 0

As you can see TRUE is coerced to 1 and FALSE is coerced to 0. This coercion also takes place when using logical values with arithmetic operators:

TRUE + TRUE + FALSE + FALSE
## [1] 2
(TRUE + TRUE) * (TRUE + TRUE)
## [1] 4
(4 > 3) + (4 < 3)
## [1] 1

The reverse coercion also takes place where numbers become logical values when used with logical operators:

1 && TRUE
## [1] TRUE
0 && TRUE
## [1] FALSE

In this case 0 is always coerced to FALSE and any number other than 0 is coerced to TRUE. More examples:

45 && 300
## [1] TRUE
0 || 3
## [1] TRUE
0 && 0 || 4 > 7
## [1] FALSE