R Programming language - R Data Structure
In R programming language, data structures are objects that hold data values of one or more types. There are several built-in data structures in R that are commonly used in data analysis and manipulation tasks. These data structures include:
Vectors: A vector is a one-dimensional array of values of the same data type, such as numbers, characters, or logical values.
Matrices: A matrix is a two-dimensional array of values of the same data type, arranged in rows and columns.
Lists: A list is a collection of objects of different types, such as vectors, matrices, or other lists.
Data frames: A data frame is a two-dimensional table-like structure, where each column can have a different data type.
Factors: A factor is a special type of vector that represents categorical data, where each value belongs to a specific category.
Arrays: An array is a multi-dimensional data structure that can hold values of the same data type.
Here is an example of how to create some of these data structures in R:
refer to:theitroad.com# Create a vector v <- c(1, 2, 3, 4) # Create a matrix m <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2) # Create a list l <- list(a = 1:3, b = matrix(c(1, 2, 3, 4), nrow = 2)) # Create a data frame df <- data.frame(id = 1:3, name = c("Alice", "Bob", "Charlie"), age = c(25, 30, 35)) # Create a factor f <- factor(c("low", "medium", "high", "low", "high")) # Create an array a <- array(1:12, dim = c(2, 3, 2))
Each data structure has its own functions and methods for manipulating and accessing the data it contains. Understanding the different data structures and their properties is an essential part of working with data in R.