In R, data structures are the objects that need to be understood completely for the efficient handling of R codes.
The various data structures available in R are:
- Atomic vector
- Data Frame
A vector in R is a collection of elements that can be understood as a basic data structure, or, the most basic R data objects.
Two types of vectors are:
- Atomic vector
Atomic Vectors are of various types such as numeric, logical, integer, character, double, and raw.
A list is a type of vector in R which is like a container. The elements in a list are not restricted to a single type. A mixture of data types in a list means each element can be a different type.
- To create a list:The list() or as.list() functions are used.
- To create an empty list: The vector() function creates an empty list of the desired length.
To store several data values of similar data types, the array data structure is utilised. The data values in an array are stored with contiguous memory allocation. On creating an array of dimensions (2, 3, 4), four rectangular matrices are created. Each matrix is of two rows and three columns.
To create an array in R:
- The array() function is utilised.
- In this function, the input is a vector.
- For creating the array, the value in the dim parameter is utilised.
The elements of a matrix are of the same data types and are arranged in a two-dimensional rectangular form.
- To create a matrix in R: The matrix() function is used.
- Syntax: matrix(data, no_row, no_col, by_row, dim_name)
A data frame in R can be understood as a table or a two-dimensional array-like structure. Each column in a data frame contains the value of one variable. The row, on the other hand, contains the set of values from each column. In R, the column names of a data frame are non-empty and row names are unique. Numeric, factor or character-type data values are stored in a data frame in R, and the number of data items is the same for every column.
The data in R can be categorized and stored as levels. To serve this purpose, factors are available as data structures. Both the strings and integers data types can be utilised in a factor object. When handling columns, factors can be very efficient, as columns have a limited number of unique values. Factor objects, in R, are thus preferred in data analysis for statistical modelling.
To create a Factor object in R:
- The factor() function can be used.
- This function takes a vector as the input.