library(help = “datasets”) mtcars ?mtcars

Descriptive statistics

head() - displays 1st 6 records

head(mtcars)

tail() - displays last 6 records

tail(mtcars)

str() - structure

str(mtcars)

summary() - summary of data

summary(mtcars)

#by() by(mtcars, mtcars$cyl, summary) ######################################################

Interquartile function IQR

IQR(mtcars$mpg)

mean, median, mode, std dev, variance

mean(mtcars\(disp) median(mtcars\)wt)

mode calculation, function() - to create new functions

getmode <- function(v) { uniqv <- unique(v) #eliminates the duplicate values in the vector uniqv[which.max(tabulate(match(v,uniqv)))] }

getmode(mtcars$cyl)

method 2 for mode cal., table() - returns frequency of each column

table(mtcars$cyl)

sort() - sort in ascending order

sort(table(mtcars\(cyl)) # sort( , decreasing =TRUE) - sort in decreasing order sort(table(mtcars\)cyl), decreasing = TRUE) #####################################################

std. dev - sd()

sd(mtcars\(drat) #variance - var() var(mtcars\)qsec) #####################################################

factor() - means categorical datapoint

factor(mtcars$cyl)

changing datatype of cyl to factor datatype

mtcars\(cyl <- factor(mtcars\)cyl) str(mtcars$cyl) summary(mtcars) ##################################################### # Accessing indexed value - accessing the Mazda RX4 row elements mtcars[‘Mazda RX4’,]

Accessing indexed value - accessing the hp column elements

mtcars[,‘hp’]

which car has highest hp? - method 1

max <- max(mtcars\(hp) mtcars\)hp == max mtcars[mtcars$hp == max,]

which car has highest hp? - method 2

order(mtcars\(hp, decreasing = TRUE) # order() - return row index value mtcars[order(mtcars\)hp, decreasing = TRUE),] head(mtcars[order(mtcars$hp, decreasing = TRUE),]) head(mtcars[order(mtcars$hp, decreasing = TRUE),], 1) # return top 1 record ################################################################## # which car has lowest hp? - method 1 min <- min(mtcars\(hp) mtcars\)hp == min mtcars[mtcars$hp == min,]

head(mtcars[order(mtcars$hp, decreasing = FALSE),])

which 8 cyl has highest mpg

max <- max(mtcars\(mpg) mtcars\)mpg == max mtcars[mtcars$mpg == max,] #########################################################