R Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

R Data Structures

R Data Structures

shape Description

Generally, a data structure is nothing but a one format to maintain the data and store the data, the general format of data structure types are tables, arrays, files, tree, records. The main functionality of data structure is to work on different algorithms. Every programming language consists of some kind of data. Datatype shows the specific data entered into a program. Data Types are the keywords used to declare the type of the variable used in the program such as integer, real, character and string. The data types are utilized inside sort frameworks, which offer different methods for characterizing, executing and utilizing them such as integer, character, string etc. The following are the types R programming objects.

R Programming Objects

shape Description

The following are the different types of R programming objects.

Vectors

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]

Matrices

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]

Lists

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

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

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]

Summary

shape Key Points

  • The array() and factor() are the functions to deal with the data.
  • The list is a heterogeneous data structure
  • The vector can also be called as atomic vector.