🥳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.
Code Example Output:

3. Array
Arrays are the R data objects which can store data in more than two dimensions.
array(data, dim, dimnames)
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)
Code 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.
Code Output:

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