The following are the different types of R programming objects.
- Vectors
- Matrices
- Lists
- Arrays
- Factors
Vectors are also commonly known as
Atomic vectors
. Atomic vectors are homogeneous data structures. Means in atomic vector, each element should be an object of same class only. So, a character vector, will have all elements of character type, while a numeric vector will have all elements of numeric type. Similarly, an integer vector will contain only objects of integer atomic class. A logical vector will contain only logical values, while a complex vector will contain objects of complex atomic class only. And moreover, the atomic vector has one dimensional arrangement of items. The following is an example for the single vector creation.
[c]# Atomic vector of type complex.
print(2+3i)[/c]
Data Frame
is arguably the most popular data structure in R. It is a heterogeneous data structure. Means this data structure can also contain elements of different classes, just like the list. However, unlike list, data frame is 2-dimensional in nature.
Matrixes
are similar to data frames as matrix also have two dimensional arrangement, just like a spreadsheet. However, unlike data frames, matrixes are homogeneous data structures. Means, a matrix can contain items of similar type only. Typically, generally use matrixes to store and process numeric data.The following is the syntax for the matrices.
[c]matrix(data, nrow, ncol, byrow, dimnames)[/c]
List
is a heterogeneous data structure. It means that user can put items of different classes or types, in a single list. And moreover, lists are one-dimensional in nature, so all elements of the list will be arrange in one dimension. The following is an example to create list.
[c]
# Create a list containing strings, numbers, vectors and a logical values.
list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
print(list_data)
[/c]
Arrays
are another type of data structure supported in R. Arrays are also homogeneous data structures just like vectors and matrixes. Means and array can only contain similar type of items. However, while vectors are one dimensional and matrixes are two dimensional, arrays on the other hand can be n-dimensional also. The
array()
is the function to create an array.
[c][/c]
Factors
are the special type of vectors which can be used to store nominal or categorical values. Categorical means the field can take a value from few categories only. The factors will be created with the function
factor()
. The following is an example.
[c]# Create a vector as input.
data <- c("East","West","East","North","North","East","West","West","West","East","North")
# Apply the factor function.
factor_data <- factor(data)
print(factor_data)
[/c]