Sec.B-Ch.1-Subsec.2:Continuing Fundamentals of Data and Core Data Structures for Clinical Data Science
A practical guide to multidimensional data storage and flexible data structures in R for medical and pharmaceutical data analysis.
R Data Structures
1. Vector
A vector is the most basic data structure in R. Almost all other data structures are built from vectors. A vector can contain data types such as numeric, character, logical, or complex values.
2. Matrix
A matrix is a two-dimensional array. Each matrix can contain only one type of data (numeric, character, or logical). A matrix can be created using the matrix() function.
3. Array
An array is an important data structure. It is somewhat similar to a matrix in that both can contain only one type of data. However, arrays have one significant characteristic: their dimensions can be greater than two, which distinguishes them from matrices.
In R, arrays are created using the array() function. This function includes several key parameters:
data: the elements used to create the array
dim: a numeric vector specifying the dimensions of the array
dimnames: a list containing names for each dimension
By setting the dim parameter appropriately, we can precisely define the dimensional structure of the array. The dimnames parameter helps assign descriptive labels to each dimension.
Example: Creating a Three-Dimensional Array
The following example demonstrates how to create a three-dimensional array named arr.data containing the values 1 to 12.
dim1 <- c("A1", "A2")
dim2 <- c("B1", "B2", "B3", "B3")
dim3 <- c("C1", "C2", "C3")
arr.data <- array(data = c(1:12), dim = c(2, 4, 3), dimnames = list(dim1, dim2, dim3))
print(arr.data)



