The “read.xlsx” Function in R
Package: openxlsx
Purpose: To read data from an Excel (.xlsx) file into a data frame.
General Class: Input/Output
Required Argument(s):
xlsxFile: A character string giving the name of the Excel file to read from.
sheet: A character string or index specifying the sheet from which to read data.
Notable Optional Arguments:
rows: An optional range of rows to read (e.g., “1:100”).
cols: An optional range of columns to read (e.g., “A:C”).
colNames: A logical value indicating whether to read column names from the first row (default is TRUE).
Example:
# Example usage
# Assume an Excel file named "data.xlsx" with a sheet named "Sheet1"
# Read the data from the specified sheet into a data frame
df <- read.xlsx("data.xlsx", sheet = "Sheet1")
# Print the structure of the data frame
str(df)In this example, the read.xlsx function from the openxlsx package is used to read data from an Excel file named “data.xlsx” and from the sheet named “Sheet1” into a data frame (df). The resulting data frame is then printed using the str function to display its structure.