The “as.data.frame” Function in R
Package: Base R (no specific package required)
Purpose: Converts an object to a data frame.
General Class: Data Manipulation
Required Argument(s):
x: An R object to be converted to a data frame.
Notable Optional Arguments:
row.names: A character vector giving the row names for the resulting data frame.
optional: A logical indicating whether conversion should be deemed successful even when the resulting data frame has different row names than x (if x is a data frame).
Example:
# Example data for using the as.data.frame function
matrix_example <- matrix(1:6, nrow = 2, ncol = 3)
# Use as.data.frame to convert a matrix to a data frame
data_frame_result <- as.data.frame(matrix_example)
# Display the result
print(data_frame_result)In this example, the as.data.frame function is used to convert a matrix (matrix_example) to a data frame (data_frame_result). The as.data.frame function is commonly used to convert various objects, such as matrices or lists, to a data frame, which is a fundamental data structure in R for tabular data.