Data Type
Last updated
Last updated
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
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.
"We can use the L
suffix to qualify any number with the intent of making it an explicit integer"
To check the type/class of a vector use class(vectorName)
.
Code Example:
Do not use more than one class of vector in a single vector.
Matrix is the R object in which the elements are arranged in a two-dimensional rectangular layout.
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:
Arrays are the R data objects which can store data in more than two dimensions.
Lists are the R objects that contain elements of different types like - numbers, strings, vectors, and other lists inside them.
In simple words, a list can contain more than one type of data.
Code Output:
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.
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:
R contains a number of preloaded datasets. You can load and use them instantly.
To view the built-in datasets in R, use the following command: data()
To load these datasets in your RStudio environment use data(datasetName)
.
To learn more about dataframe and their manipulation view the following page: