Question: # function to clear the console, plots and environment clear _ all < - function ( ) { # clear console cat (

# function to clear the console, plots and environment
clear_all <- function(){
# clear console
cat("\014")
# clear plots
if (dev.cur()>1) dev.off()
while (dev.cur()>1) dev.off()
# clear environment (global environment)
rm(list = ls(envir =.GlobalEnv), envir =.GlobalEnv)
message("Console, Plots, and Environment cleared!")
}
#----------------------------------------------------------------------------------------------
# function to install and load packages
install_and_load_packages <- function(packages){
# check for packages that are not installed
new_packages <- packages[!(packages %in% installed.packages()[, "Package"])]
# install missing packages
if (length(new_packages)){
install.packages(new_packages)
message("The following packages were installed: ", paste(new_packages, collapse =","))
} else {
message("All packages are already installed.")
}
# load all the packages
sapply(packages, require, character.only = TRUE)
}
#----------------------------------------------------------------------------------------------
# example usage
clear_all()
packages_to_install <- c("ggplot2", "GGally", "PerformanceAnalytics", "ggcorrplot")
install_and_load_packages(packages_to_install)
#----------------------------------------------------------------------------------------------
# simple scatter plot using base R
x <- c(1,2,3,4,5)
y <- c(5,4,3,2,1)
plot(x, y, main="Basic Scatter Plot",
xlab="X-axis", ylab="Y-axis", col="red",
pch=19)
#----------------------------------------------------------------------------------------------
# example of a simple scatter plot using ggplot2
data <- data.frame(x = c(1,2,3,4,5),
y = c(5,4,3,2,1))
#install.packages("ggplot2") # or enter the package name in packages_to_install above
#library(ggplot2)
ggplot(data, aes(x = x, y = y))+
geom_point(color="blue")+
labs(title="Scatter Plot with ggplot2", x="X-axis", y="Y-axis")
#----------------------------------------------------------------------------------------------
# 1. data exploration
#----------------------------------------------------------------------------------------------
# load the built-in mtcars dataset (there are several datasets in R, such as: iris, airquality, etc)
data(mtcars)
# dataset description
?mtcars
# get the dimensions of the dataset: rows, cols
dimensions <- dim(mtcars)
# print the number of rows and columns
cat("Number of rows:", dimensions[1])
cat("Number of columns:", dimensions[2],
"
","(mtcars dataset)")
print(paste("Number of columns:", dimensions[2]))
# print column names
print("Column Names:")
colnames(mtcars)
# print row names
print("Row Names:")
rownames(mtcars)
# print the first few rows
print("First Few Rows:")
head(mtcars)
# print the first 20 rows
head(mtcars,20)
# print the last few rows
print("Last Few Rows:")
tail(mtcars)
# print the entire dataset
print("Entire Dataset:")
print(mtcars)
# print column names and their data types
print("Structure of Dataset:")
str(mtcars)
# summary statistics of all columns
summary(mtcars)
# print specific rows and columns
print(mtcars[1:5,1:3]) #(e.g., rows 1 to 5 and columns 1 to 3)
print(mtcars[,1]) #first col values
print(mtcars[2,]) #second row values
print(mtcars[2,1]) #second row, first col value
# subset the mtcars dataset with multiple conditions
subset_mtcars <- subset(mtcars, mpg >20 &
hp <150 &
cyl ==4)
subset_mtcars # we need to run this line to visualize the result stored in subset_mtcars df
# calculate and display unique values for each column
sapply(mtcars, unique)
# calculate the range for each numeric column, ignoring NA values
sapply(mtcars, function(col) range(col, na.rm = TRUE))
#----------------------------------------------------------------------------------------------
# 2. missing values (in R, NA stands for "not available")
#----------------------------------------------------------------------------------------------
# check if any NA values are present in the dataset
any(is.na(mtcars))
# count total NA values in the dataset
sum(is.na(mtcars))
#-----
# there are no NA values in mtcars dataset
# introducing NA in a new dataset: my_dataset
rownames(mtcars)<-

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!