R Data Types
A data type is the kind of a value, such as a number or a piece of text. R checks the type of each value before it works with the value.
Three kinds by class
class reports the type of each value. The three values give three different types.
print(class(5))
print(class(5L))
print(class("Hello"))Two more kinds
class shows TRUE as a logical value. The paired value prints and reports as complex.
print(class(TRUE))
print(2 + 3i)
print(class(2 + 3i))Integer needs the L mark
A plain number is not an integer. The same value with an L mark is an integer.
print(is.integer(5))
print(is.integer(5L))
print(2.0)Text plus a number fails
Text and a number try to add together. R stops with an error for the mix.
"a" + 1Example
price <- 9.5
count <- 4L
label <- "Fresh"
print(class(price))
print(class(count))
print(class(label))Key points
- class() reports the type of a value.
- A plain number like 5 has type numeric.
- An integer ends its value with an L, as in 5L.
- Text values have type character.
- The words TRUE and FALSE have type logical.
- A complex value pairs two parts, as in 2 + 3i.
- Text and numbers do not add, and the mix stops with an error.
In this example three names hold a numeric value, an integer, and a text value. The class calls print the type of each stored value.
Note
NA is the marker R uses for a missing value.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท rdrr.io R Snippets
Exercise
Which value has type integer?