Atomic Vectors in R
Arrays and matrices are built on the atomic vector, R’s most basic data structure. Atomic vectors are one-dimensional data sets with the same data type or mode. This single-type rule renders them “atomic”. R Programming treats a scalar as an atomic vector of length one. The six basic atomic vectors are doubles (numerics), integers, characters, logicals, complex, and raw. Concatenating values with the c() function is the most popular approach to generate a vector.
You can build a numeric vector with c(1, 2.5, -100), a character vector with c(“Hello”, “World”), and a logical vector with c(TRUE, FALSE). Atomic vectors coerce: if you combine data types with c(), R will convert all items to the most flexible type to maintain the single-type rule. Logicals are translated to numerics (TRUE becomes 1, FALSE becomes 0), which are then turned to characters if the vector contains text strings. R relies on this structure to vectorize, which efficiently applies operations to each vector element at once, resulting in compact and fast code.
Creating Vectors with the c() Function
The “concatenate” function c() is the simplest basic vector-creating tool in R. You may also use this method to “collect” or “combine” values into a one-dimensional object. R programming relies on vectors, ordered collections of pieces of the same data type (numeric, character, or logical). Named “atomic vectors” due to their single-type rule. The c() method is simple: specify the things you want to include in parentheses, separated by commas. Numeric vectors are created by grouping integers, character vectors by grouping strings in quotation marks, and logical vectors by grouping TRUE and FALSE values.
R treats even a single number or string as a one-element vector, therefore the c() function concatenates these little vectors into a bigger one. The “flattening” effect of this function turns a vector into a single, non-recursive vector. How c() handles several data types in a call is crucial. R will coerce the elements into the least common denominator from logical to integer, numeric (double), and character since an atomic vector can only have one mode. If you combine numbers, logicals, and character strings, R will convert them all to character strings to maintain vector consistency.
Numeric Vectors: Doubles and Integers
Doubles and integers are R’s basic atomic vector types for numeric data, the most popular kind in statistical computing.
Doubles: Double vectors are the most frequent way to store normal integers in R. Doubles, sometimes known as “numeric” vectors, can hold positive, negative, large, tiny, and decimal numbers. Numbers typed in R are usually kept as doubles by default. A single scalar is handled as a one-length double vector. For vectors, typeof() returns “double” to check data type. The internal type is “double,” yet the object’s class is “numeric”.
Integers: In R, integer vectors store non-decimal integers. A separate integer vector is needed for precision and memory management, even though the double (or “numeric”) vector can store whole numbers. R stores entire numbers as doubles until you enter them with an uppercase L to produce integers. L is an integer, while 4 is a double, and this difference is only in how R stores the number in the computer’s memory. A vector with the L suffix returns “integer” when typeof() is called.
Example:
# Doubles (default numeric type in R)
x <- c(1, 2.5, -3, 4)
cat("Vector x (doubles):\n")
print(x)
cat("Type of x:", typeof(x), "\n")
cat("Class of x:", class(x), "\n\n")
# Integers (use L suffix)
y <- c(1L, 2L, 3L, 4L)
cat("Vector y (integers):\n")
print(y)
cat("Type of y:", typeof(y), "\n")
cat("Class of y:", class(y), "\n\n")
# Single number check
a <- 4
b <- 4L
cat("a =", a, " | typeof(a):", typeof(a), "\n")
cat("b =", b, " | typeof(b):", typeof(b), "\n")
Output:
Vector x (doubles):
[1] 1.0 2.5 -3.0 4.0
Type of x: double
Class of x: numeric
Vector y (integers):
[1] 1 2 3 4
Type of y: integer
Class of y: integer
a = 4 | typeof(a): double
b = 4 | typeof(b): integer
Character Vectors
Character vectors in R are atomic vectors that store text. Character vectors contain strings. Enclose each string in single (‘) or double (“) quotation marks to produce a character vector. Use the c() method to combine several texts. A string can contain letters, numbers, and symbols; R treats anything in quotes as a character string. This is important since “5” is a character string and 5 is a number.
If you try to combine a character string with numbers or logicals, R will convert all elements into character strings because atomic vectors can only hold one data type. Since character is the most flexible coercion type, this happens. Beginners often confuse a R object name (e.g., x) with a character string including a letter (e.g., “x”). Forgetting the quotation marks may lead R to search for an object that may not exist, resulting in an error. Typeof() and mode() return “character” to validate that a vector is a character type.
Logical Vectors
Logical vectors, a basic atomic vector type in R, store Boolean data that can only be TRUE or FALSE. For logical data, these values must be stated in all capital letters and without quotation marks. R also assumes T and F are shorthand for TRUE and FALSE, but using the full words is better. Use the c() function to build logical vectors, such as <- c(TRUE, FALSE, TRUE). They usually result from comparisons and logical checks. The logical operators ==,!=, <, >, and %in% in R conduct element-wise comparisons on vectors, yielding a logical vector of the results.
Logical subsetting uses a vector of TRUEs and FALSEs as an index to quickly find, extract, and edit values in another object that match the TRUE positions. Multiple logical tests can be coupled using Boolean operators like & (AND) and | to create more complicated criteria. Coercion: logicals are translated to integers in arithmetic, with TRUE becoming 1 and FALSE 0. This is useful since sum() can count TRUEs in a vector and mean() can calculate their proportion.
The “Atomic” Rule: Coercion
Atomic vectors in R Programming can only store one type of data. This means all elements in an atomic vector must be numeric, character, or logical. R uses coercion to enforce this “atomic” constraint when combining data types into a vector. To preserve vector atomicity, coercion automatically converts items to a single data type. R follows process-specific rules. When a vector contains a character string, R converts all other items into strings.
Combining “ace”, “hearts”, and 1 creates a character vector that matches “1”. A vector of only logicals and integers will be converted to numerics by R, with TRUE becoming 1 and FALSE 0. This hierarchy (logical -> integer -> numeric -> complex -> character) preserves information predictably. Matrix and array are special examples of atomic vectors and can only hold one data type, hence this behavior applies.