Question: # function to clear the console, plots and environment clear _ all < - function ( ) { # clear console cat (
# function to clear the console, plots and environment
clearall function
# clear console
cat
# clear plots
if devcur dev.off
while devcur dev.off
# clear environment global environment
rmlist lsenvir GlobalEnv envir GlobalEnv
messageConsole Plots, and Environment cleared!"
#
# function to install and load packages
installandloadpackages functionpackages
# check for packages that are not installed
newpackages packagespackages in installed.packages "Package"
# install missing packages
if lengthnewpackages
install.packagesnewpackages
messageThe following packages were installed: pastenewpackages, collapse
else
messageAll packages are already installed."
# load all the packages
sapplypackages require, character.only TRUE
#
# example usage
clearall
packagestoinstall cggplot "GGally", "PerformanceAnalytics", "ggcorrplot"
installandloadpackagespackagestoinstall
#
# simple scatter plot using base R
x c
y c
plotx y main"Basic Scatter Plot",
xlabXaxis", ylabYaxis", col"red",
pch
#
# example of a simple scatter plot using ggplot
data data.framex c
y c
#install.packagesggplot # or enter the package name in packagestoinstall above
#libraryggplot
ggplotdata aesx x y y
geompointcolor"blue"
labstitle"Scatter Plot with ggplot xXaxis", yYaxis"
#
# data exploration
#
# load the builtin mtcars dataset there are several datasets in R such as: iris, airquality, etc
datamtcars
# dataset description
mtcars
# get the dimensions of the dataset: rows, cols
dimensions dimmtcars
# print the number of rows and columns
catNumber of rows:", dimensions
catNumber of columns:", dimensions
mtcars dataset
printpasteNumber of columns:", dimensions
# print column names
printColumn Names:"
colnamesmtcars
# print row names
printRow Names:"
rownamesmtcars
# print the first few rows
printFirst Few Rows:"
headmtcars
# print the first rows
headmtcars
# print the last few rows
printLast Few Rows:"
tailmtcars
# print the entire dataset
printEntire Dataset:"
printmtcars
# print column names and their data types
printStructure of Dataset:"
strmtcars
# summary statistics of all columns
summarymtcars
# print specific rows and columns
printmtcars:: #eg rows to and columns to
printmtcars #first col values
printmtcars #second row values
printmtcars #second row, first col value
# subset the mtcars dataset with multiple conditions
subsetmtcars subsetmtcars mpg &
hp &
cyl
subsetmtcars # we need to run this line to visualize the result stored in subsetmtcars df
# calculate and display unique values for each column
sapplymtcars unique
# calculate the range for each numeric column, ignoring NA values
sapplymtcars functioncol rangecol narm TRUE
#
# missing values in R NA stands for "not available"
#
# check if any NA values are present in the dataset
anyisnamtcars
# count total NA values in the dataset
sumisnamtcars
#
# there are no NA values in mtcars dataset
# introducing NA in a new dataset: mydataset
rownamesmtcars
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
