Every programming language has its data types, and almost all programming languages share some common data types like Integer, String, Character, Array, etc. R has an extensive type of data types, including vectors (numerical, character, logical), matrices, data frames, arrays, factors, and lists.
R Data Types
In the R language, the variables are not declared as some data type. The variables are assigned with R-Objects, and the data type of the R-object becomes the data type of the variable, unlike other programming languages.
R lang has various R-objects, which are the following.
Among all the above R-objects, the vector is the simplest data type in R language.
R Vector Data type
A vector in R is a series of data items of the same basic type. Members in a vector are called components. When you want to create a vector with more than one element, use the c() function, combining them into a single vector.
x <- c(11, 21, 5.2, 6, -19, 0.46) y <- c("eleven", "eight", "one") z <- c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE) x typeof(x) y typeof(y) z typeof(z)
 11.00 21.00 5.20 6.00 -19.00 0.46  "double"  "eleven" "eight" "one"  "character"  TRUE FALSE TRUE FALSE TRUE FALSE  "logical"
We have created three types of vectors using the c() function. To check the R object’s data type, use the typeof() function.
There are six types of vector objects available in R.
- Logical vector
- Numeric vector
- Integer vector
- Complex vector
- Character vector
- Raw vector
A logical vector is a vector that only contains TRUE and FALSE values, which means it only contains boolean values.
logical_rv <- TRUE logical_rv class(logical_rv)
 TRUE  "logical"
The class() function determines the class of an object or its “internal” type.
The numeric vectors create a real vector of the specified length. The elements of the vector are all equal to 0. The numeric vectors can have values like 11, 21.19, 1.1, etc.
numeric_rv <- 11.21 numeric_rv class(numeric_rv)
 11.21  "numeric"
And we get the numeric vector. It has as.numeric() method, which is a generic function. To check if it is a numeric value or not, use the is.numeric() method.
The integer() method creates or tests for objects of type “integer“. The current implementation of R uses the 32-bit integers for integer vectors. Integer vectors can have values like 21L, 19L, 0L, etc.
integer_rv <- 21L integer_rv class(integer_rv)
 21  "integer"
The as.integer() function tries to coerce its argument to be of integer type. To check if the value is an integer or not, use the is.integer() function.
The complex vector in R can be specified either by providing its length, its real and imaginary parts, or modulus and argument. The complex vector can have values like 1 + 2i, 2 + 5i, 1 + 0i, etc.
complex_rv <- 19 + 21i complex_rv class(complex_rv)
 19+21i  "complex"
The as.complex() function tries to control its argument to be of complex type. To check if the data type is complex or not, use the is.complex() method. All forms of NA and NAN are coerced to a complex NA, for which both the real and imaginary parts are NA.
Text in R is represented by character vectors. A character vector is a vector consisting of characters. To create a character vector, use the characters in double or single-quotes. You can have character vector values like ‘x‘ , “data”, “TRUE“, ‘21.19‘.
character_rv <- "a + b + c" character_rv class(character_rv)
 "a + b + c"  "character"
You can assign a value to a character vector using the assignment operator (<-), the same way you do for all other data types variables. To find out the length of the vector, use the length() function. To check if the variable holds a character vector or not, use the is.character() function.
The raw vector data type is intended to hold raw bytes. A raw vector is printed with each byte separately represented as a pair of hex digits. If you want to see a character representation (with escape sequences for non-printing characters), use the rawToChar() function.
The “Hello” is stored as 48 65 6c 6c 6f as raw values. Let’s see with examples.
raw <- raw(8) raw class(raw) raw_rv <- charToRaw("Database") raw_rv class(raw_rv)
 00 00 00 00 00 00 00 00  "raw"  44 61 74 61 62 61 73 65  "raw"
To convert raw to a character in R, use the rawToChar() function.
raw_rv <- charToRaw("Database") raw_rv class(raw_rv) char_rv <- rawToChar(raw_rv) char_rv class(char_rv)
 44 61 74 61 62 61 73 65  "raw"  "Database"  "character"
The as.raw() function tries to curb its argument to be of raw type. The answer will be 0 unless the coercion succeeds.
The is.raw() function returns TRUE if and only if typeof(x) == “raw”.
List in R
A list is an R-object containing different types of elements inside it like vectors, functions, and even another list inside it. A list is an ordered collection of elements under one name.
r_list <- list(c(11, 19, 21), 19.3, cos, TRUE) r_list
[]  11 19 21 []  19.3 [] function (x) .Primitive("cos") []  TRUE
To access list elements one by one, use the [[ ]] brackets. Let’s access the third element of the list. Remember, the list index starts at 1 and not 0.
r_list <- list(c(11, 19, 21), 19.3, cos, TRUE) r_list[]
function (x) .Primitive("cos")
Factor in R
The factor stores the nominal values as a vector of integers in the specific range and an internal vector of character strings mapped to these integers.
rv <- c(rep("adam", 3), rep("eve", 4)) r_factor <- factor(rv) r_factor
 adam adam adam eve eve eve eve Levels: adam eve
R treat factors as nominal variables and ordered factors as ordinal variables in statistical procedures and graphical analyses.
Matrix in R
A matrix is a two-dimensional rectangular data structure that can be created using a vector input to the matrix function. The as.matrix() function attempts to turn its argument into the matrix. The is.matrix() function tests if its argument is a (strict) matrix.
mtrx <- matrix(c("a", "b", "c", "d"), nrow = 2, ncol = 2, byrow = TRUE) mtrx
[,1] [,2] [1,] "a" "b" [2,] "c" "d"
Array in R
Array in R is a list or vector with two or more dimensions. An array is like a stacked matrix, and a matrix is a two-dimensional array.
rv <- c(11, 19, 18) rv2 <- c(21, 6, 29, 46, 37, 38) result <- array(c(rv, rv2), dim = c(3, 3, 2)) print(result)
, , 1 [,1] [,2] [,3] [1,] 11 21 46 [2,] 19 6 37 [3,] 18 29 38 , , 2 [,1] [,2] [,3] [1,] 11 21 46 [2,] 19 6 37 [3,] 18 29 38
Data Frame in R
Data Frame in R is a table or two-dimensional array-like structure in which a row contains a set of values, and each column holds values of one variable.
streaming <- data.frame( service_id = c(1:5), service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"), service_price = c(18, 10, 15, 7, 12), stringsAsFactors = FALSE ) # Print the data frame. print(streaming)
service_id service_name service_price 1 1 Netflix 18 2 2 Disney+ 10 3 3 HBOMAX 15 4 4 Hulu 7 5 5 Peacock 12
Unlike a matrix, in a data frame, each column can contain different modes of data. Data Frames are created using the data.frame() function.
Useful inbuilt R Functions
Inbuilt functions are also the data types in R.
length(object) str(object) class(object) names(object) c(object,object,...) cbind(object, object, ...) rbind(object, object, ...)
That is it for the R Data Types example.
Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.