R data types
last modified November 19, 2026
Every value in R has a type, and the type decides which operations are allowed and how the value is printed. Base R has six atomic types, and they are the building blocks of every vector, list, and data frame. This article describes the six types, the functions that inspect them, the rules that convert one type into another, and the special values that mark missing or undefined results.
The six atomic types
A value written directly in source code is a literal, and its type is
fixed by its form. The suffix L marks an integer, decimal digits
without a suffix form a double, and quoted text forms a character string.
typeof returns the internal type as a string. The six atomic
types are the ones whose elements are single values rather than other R
objects.
print(typeof(TRUE))
print(typeof(1L))
print(typeof(2.5))
print(typeof("text"))
print(typeof(3 + 4i))
print(typeof(as.raw(65)))
The first value is logical, the second integer, the third double, the fourth
character, the fifth complex, and the last raw. A double is a
floating-point number stored in 64 bits, a complex number has a real
and an imaginary part, and a raw value is one byte. The table lists
the same types with a literal for each one and the string that
typeof returns.
| Type | Literal | typeof |
|---|---|---|
| logical | TRUE | "logical" |
| integer | 42L | "integer" |
| double | 2.5 | "double" |
| character | "text" | "character" |
| complex | 3 + 4i | "complex" |
| raw | as.raw(65) | "raw" |
$ Rscript types.R [1] "logical" [1] "integer" [1] "double" [1] "character" [1] "complex" [1] "raw"
typeof, class, and mode
Three functions describe the type of an object, and they do not always agree.
typeof reports the internal storage type, class
reports the class used for method dispatch, and mode is an older
function kept for compatibility. For an integer vector, the first two report
integer and the last reports numeric, because
mode groups both integer and double under one name.
x <- c(1L, 2L, 3L)
cat("typeof:", typeof(x), "\n")
cat("class:", class(x), "\n")
cat("mode:", mode(x), "\n")
The difference matters when a function checks its input: is.numeric
accepts integers and doubles, while is.integer accepts only
integers. A class can also be set explicitly, which is how data frames,
factors, and model objects get their own behaviour; the object-oriented
articles later in the series build on that mechanism.
$ Rscript classmode.R typeof: integer class: integer mode: numeric
Logical values
The logical type has two values, TRUE and FALSE, plus
NA for a value that is missing. Logical vectors are the result of
comparisons, and they drive filtering and counting. In arithmetic,
TRUE is treated as one and FALSE as zero, so
sum over a logical vector counts the true values.
print(TRUE)
print(FALSE)
print(c(TRUE, FALSE, NA))
print(sum(c(TRUE, FALSE, TRUE)))
print(is.logical(TRUE))
print(as.logical(1))
print(as.logical("FALSE"))
as.logical converts zero to FALSE and any other finite
number to TRUE; a missing value stays missing. It also recognises the strings
"TRUE" and "FALSE", which is useful when data has
been read from a text file. The abbreviations T and
F exist, but they are ordinary variables and can be overwritten,
so write the full names.
$ Rscript logicals.R [1] TRUE [1] FALSE [1] TRUE FALSE NA [1] 2 [1] TRUE [1] TRUE [1] FALSE
Integers
An integer literal ends with L, as in 42L. Without
the suffix, 42 is a double, which surprises readers who expect
whole numbers to be integers by default. Integers are stored in 32 bits with a
range from -2147483648 to 2147483647; arithmetic that leaves that range
produces NA and a warning.
x <- 42L
cat("x =", x, "\n")
cat("typeof:", typeof(x), "\n")
cat("is.integer:", is.integer(x), "\n")
print(as.integer(3.9))
print(as.integer("17"))
Converting a double to an integer truncates toward zero: 3.9 becomes 3, and
-3.9 would become -3. Converting a string parses the whole string and fails
with NA when the text is not a number. The predicate
is.integer distinguishes integers from doubles, which matters
because the two types print differently in some functions.
$ Rscript integers.R x = 42 typeof: integer is.integer: TRUE [1] 3 [1] 17
Doubles
A double is the default numeric type. A literal may use a decimal point, a
power of ten, or both: 2.5, 1e3, and
1.5e-3 are all doubles. Doubles follow the IEEE 754 standard,
which defines how the sign, the exponent, and the fraction are packed into 64
bits and therefore how much precision a calculation keeps.
print(2.5) print(1e3) print(1 / 3) print(sqrt(2)) print(is.double(2.5)) print(is.integer(2.5))
Division always produces a double, even when both operands are integers, so
1 / 3 prints seven significant digits by default. The functions
is.double and is.integer are exact tests; the broader
predicate is.numeric accepts both types. The distinction between
numeric and double is mostly historical: in
class and mode, an ordinary double is reported as
numeric.
$ Rscript doubles.R [1] 2.5 [1] 1000 [1] 0.3333333 [1] 1.414214 [1] TRUE [1] FALSE
Floating-point precision
A double keeps about 15 to 17 significant decimal digits, and most decimal
fractions have no exact binary representation. The sum of 0.1 and 0.2 is
therefore not exactly 0.3, even though it prints as 0.3 at the default
precision. The function print accepts a digits
argument that shows more of the stored value.
print(0.1 + 0.2, digits = 17) print(0.1 + 0.2 == 0.3) print(all.equal(0.1 + 0.2, 0.3))
The comparison with == is false because the two doubles differ in
their last bits, while all.equal returns true because it compares
with a small tolerance. The lesson applies to every language that uses binary
floating point: do not test computed doubles for exact equality. Compare the
difference with a tolerance, or use all.equal, which is designed
for that purpose.
$ Rscript float.R [1] 0.30000000000000004 [1] FALSE [1] TRUE
Special numeric values
IEEE 754 reserves three special values. Inf and
-Inf represent positive and negative infinity, and
NaN means not a number, the result of an operation such as zero
divided by zero. Inf is a valid double and can be compared and
used in arithmetic, but it is not finite.
print(1 / 0) print(-1 / 0) print(0 / 0) print(is.finite(1 / 0)) print(is.nan(0 / 0)) print(is.nan(NA)) print(is.na(NA))
is.finite is false for both infinities, and is.nan
recognises the result of an undefined operation. A missing value is different:
is.nan(NA) is false because NA means that the value
is unknown, not that the operation was undefined. is.na is true
for both, which is why it is the safer test when data may contain either.
$ Rscript special.R [1] Inf [1] -Inf [1] NaN [1] FALSE [1] TRUE [1] FALSE [1] TRUE
Characters
A character value is a string of text in double or single quotes. Escape
sequences begin with a backslash: \n is a newline,
\t a tab, \" a double quote inside a double-quoted
string, and \\ a literal backslash. nchar counts the
characters in a string, and as.character converts a value of
another type to text.
name <- "R"
cat("name:", name, "\n")
cat("typeof:", typeof(name), "\n")
cat("length:", nchar(name), "\n")
print(as.character(42))
The name length in the printed text is just a label; the function
that counts characters is nchar. The conversion of the number 42
produces the string "42", which print shows with
quotes. Joining strings, splitting them, and replacing parts are covered in
the strings article later in the series.
$ Rscript characters.R name: R typeof: character length: 1 [1] "42"
Complex numbers
A complex number is written as a real part followed by an imaginary part and
the letter i, as in 3 + 4i. The functions
Re and Im return the two parts, Mod
returns the distance from the origin, Arg returns the angle in
radians, and Conj returns the complex conjugate.
z <- 3 + 4i
cat("z =", z, "\n")
cat("Re(z) =", Re(z), "\n")
cat("Im(z) =", Im(z), "\n")
cat("Mod(z) =", Mod(z), "\n")
cat("Conj(z) =", Conj(z), "\n")
The modulus is five because 3-4-5 is a right triangle, and the conjugate changes the sign of the imaginary part. Complex numbers are part of base R and appear in signal processing and in the roots of polynomials, but most data analysis never needs them. They are included here because they are one of the six atomic types, not because they are common in everyday work.
$ Rscript complex-numbers.R z = 3+4i Re(z) = 3 Im(z) = 4 Mod(z) = 5 Conj(z) = 3-4i
Raw bytes
A raw value is a single byte, printed as two hexadecimal digits. Raw vectors
carry binary data such as compressed files, hashes, or the contents of an
image. charToRaw converts a string to bytes, as.raw
builds a raw vector from numbers between 0 and 255, and rawToChar
converts bytes back to text.
print(charToRaw("AB"))
print(as.raw(c(65, 66)))
print(rawToChar(as.raw(c(65, 66))))
The bytes 65 and 66 are the ASCII codes of A and B,
and they print as 41 and 42 in hexadecimal. A number outside the range 0 to
255 is replaced by zero and produces a warning, so a byte cannot silently wrap
around. Raw vectors have no missing-value marker, which is one reason they are
used for binary formats rather than for ordinary data.
$ Rscript raw-bytes.R [1] 41 42 [1] 41 42 [1] "AB"
Coercion
When values of different types meet in one vector, R converts them to a common
type. The conversion rule is a hierarchy: logical values become integers,
integers become doubles, doubles become complex, and every type becomes
character. This automatic conversion is called coercion, and the
explicit functions such as as.integer perform the same operation
on purpose.
x <- c(TRUE, 1L, 2.5) print(x) print(typeof(x)) y <- c(1, 2, "three") print(y) print(typeof(y))
The first vector contains a logical, an integer, and a double, so all three
become doubles and the logical TRUE becomes 1. The second vector
contains numbers and a string, so the numbers become strings and the quotes
appear in the output. The hierarchy is the reason a single string in a numeric
column turns every value of that column into text.
$ Rscript coercion.R [1] 1.0 1.0 2.5 [1] "double" [1] "1" "2" "three" [1] "character"
Missing values
NA is a missing value, and it has a type even though it prints
without one. The bare NA is logical, and the typed forms
NA_integer_, NA_real_, NA_character_,
and NA_complex_ belong to the other atomic types. Using the typed
form keeps a vector numeric when a value is missing.
print(typeof(NA)) print(typeof(NA_integer_)) print(typeof(NA_real_)) print(typeof(NA_character_)) print(is.na(c(1, NA, 3)))
is.na is vectorised: it returns one logical value per element and
marks every missing position. Arithmetic with NA produces
NA, and many functions return NA unless they are told
to remove missing values with na.rm = TRUE. The
missing-values article later in the series covers that argument, along with
NULL, which is the absence of an object rather than a missing
element.
$ Rscript nas.R [1] "logical" [1] "integer" [1] "double" [1] "character" [1] FALSE TRUE FALSE
Checking and inspecting types
The is. functions are predicates: they return a single
TRUE or FALSE that answers a question about a value.
is.numeric accepts both integers and doubles,
is.double accepts only doubles, and is.atomic accepts
any of the six types. str prints a compact description of an
object, including its type, its length, and the first few values.
x <- c(1.5, 2.5, 3.5) print(is.numeric(x)) print(is.double(x)) print(is.integer(x)) print(is.atomic(x)) print(is.vector(x)) str(x)
The vector is numeric and double but not integer, because the literals have
decimal points and no L suffix. str writes
num for a numeric vector and shows the range [1:3]
before the values. The same functions work on longer vectors and on the
columns of a data frame, which makes them the first tools to reach for when an
imported file has the wrong type.
$ Rscript predicates.R [1] TRUE [1] TRUE [1] FALSE [1] TRUE [1] TRUE num [1:3] 1.5 2.5 3.5
Next steps
The operators article shows how values of these
types are combined with arithmetic, comparison, and logic. The vectors article
later in the series explains how to build, index, and modify vectors, and the
missing-values article covers NA, NaN, and
NULL in more detail.
Source
The R Language Definition: basic types
An Introduction to R: basic data types
In this article we have met the six atomic types of R, inspected them with
typeof, class, and str, and converted
between them with the as. functions. We saw how coercion picks a
common type for a vector, how doubles handle precision and special values, and
how typed NA values keep a vector numeric. The series continues
with R operators.
Author
List all R Language tutorials.