The “rownames” Function in R
Package: Base R (no specific package required)
Purpose: Gets or sets the row names of matrices or data frames.
General Class: Data Manipulation
Required Argument(s):
A matrix or data frame for which row names are to be retrieved or set.
Notable Optional Arguments:
value: If specified, sets the row names of the matrix or data frame to the specified values.
Example:
# Example data for using the rownames function
matrix_example <- matrix(1:6, nrow = 2, ncol = 3)
# Set row names for a matrix
rownames(matrix_example) <- c("Row1", "Row2")
# Get row names of the matrix
row_names <- rownames(matrix_example)
# Display the result
print(row_names)In this example, the rownames function is used to set row names for a matrix (matrix_example) and then retrieve those row names. The resulting row_names vector contains the names “Row1” and “Row2.” The rownames function is specific to matrices and data frames, allowing for the manipulation of row names in these structures.