🥳Data Type
A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational, or logical operations can be applied to it without causing an error.
There are mainly 5 data types in R:
Vector
Matrix
Array
List
Data Frame
1. Vector
A vector is a sequence of data elements of the same basic type. The are 5 classes of vectors.
In R vectors are denoted by
c().
Logical
Ex: True or False
Integer: The whole number values.
Ex: 1, 2, 5, 100, 20L, 15L, etc.
Numeric: Both whole numbers and decimal values.
Ex: 4, 3.1416, 0.534, etc.
Complex
Ex: 3+4i, 5+2i, etc.
Character: Needs to be enclosed between single or double quotes.
Ex: "M", "We", "Someone", etc.
Code Example:
VactorName <- c("We", "love", "R", "programming")Do not use more than one class of vector in a single vector.
2. Matrix
Matrix is the R object in which the elements are arranged in a two-dimensional rectangular layout.
matrix(data, nrow, ncol, byrow, dimnames)Syntax Breakdown:
data: is the input vector which becomes the data elements of the matrix.
nrow: is the number of rows to be created.
ncol: is the number of columns to be created.
byrow: is a logical clue. If TRUE then the input vector elements will be arranged by row.
dimnames: are the names assigned to the rows and columns.
# By default byrow = FALSE
matr <- matrix(c(5:29),5,5)
# When byrow = TRUE
matr <- matrix(c(5:29),5,5, byrow = TRUE)
Code Breakdown:
c(5:29): is the data, which creates a sequence of numbers from 5 to 29.Then by
5,5we denote to create a matrix with 5x5 dimension.
Code Example Output:

3. Array
Arrays are the R data objects which can store data in more than two dimensions.
array(data, dim, dimnames)arr <- array(c(0:15), dim = c(4,4,2,2))Code Breakdown:
c(0:15): is the data, which creates a sequence of numbers from 0 to 15c(4,4,2,2):is the dimension (dim),4,4denotes a matrix of 4x4 dimension, then2,2denotes an array of 2x2 dimensions with that matrix.
4. List
Lists are the R objects that contain elements of different types like - numbers, strings, vectors, and other lists inside them.
listName <- list(data)# Let's create some vectors with different Data type
# Integer
vctr1 <- c(1:5)
# Character
vctr2 <- c("I", "love", "R", "programming")
# Logical
vctr3 <- c(TRUE, FALSE)
# Create a list
aList <- list(vctr1, vctr2, vctr3)
# View the list
aListCode Output:

4. DataFrame
A dataframe is a table or two-dimensional array-like structure in which each column contains values of one variable and each row contains one set of values from each column.
data.frame(data, row.names = NULL, stringsAsFactors = FALSE)Syntax Breakdown:
data: can be a matrix, table, etc.
row.names = NULL: Whether you want to specify a column name that will be used as row names. Unless specified (NULL) row names will be integer numbers.stringsAsFactors = FALSE: If TRUE, the columns with character values will be considered as a factor.
# Let's create some vector
vctr1 <- c(1:5)
vctr2 <- c("Rahim", "Karim", "Jodu", "Modu", "Neymar")
vctr3 <- c(14,16,78,23,24)
# Now, use these vectors to create a dataframe
df <- data.frame(vctr1, vctr2, vctr3)
# View the dataframe
dfCode Output:

To learn more about dataframe and their manipulation view the following page:
⚽DataFrameSources of the contents on this page:
Last updated

