# Installing and loading necessary packages
# If you don't have the 'xlsx' package installed yet, run this command:
# install.packages("xlsx")
library(xlsx)
# Verifying installation and version
packageVersion("xlsx")
## [1] '0.6.5'
# Creating data for xlsx file
# Let's create a data frame with three columns: Name, Age, and Gender
name <- c("Alice", "Bob", "Charlie", "David", "Eve")
age <- c(22, 31, 45, 27, 19)
gender <- c("F", "M", "M", "M", "F")
df <- data.frame(Name = name, Age = age, Gender = gender)
# Saving data to xlsx file
write.xlsx(df, "example_data.xlsx", sheetName = "Sheet1")
# Reading data from xlsx file
# Load the data from the xlsx file into a new data frame
df_from_xlsx <- read.xlsx("example_data.xlsx", sheetName = "Sheet1")
# Display the loaded data
print(df_from_xlsx)
## NA. Name Age Gender
## 1 1 Alice 22 F
## 2 2 Bob 31 M
## 3 3 Charlie 45 M
## 4 4 David 27 M
## 5 5 Eve 19 F