library(help = “datasets”) mtcars ?mtcars
head(mtcars)
tail(mtcars)
str(mtcars)
summary(mtcars)
#by() by(mtcars, mtcars$cyl, summary) ######################################################
IQR(mtcars$mpg)
mean(mtcars\(disp) median(mtcars\)wt)
getmode <- function(v) { uniqv <- unique(v) #eliminates the duplicate values in the vector uniqv[which.max(tabulate(match(v,uniqv)))] }
getmode(mtcars$cyl)
table(mtcars$cyl)
sort(table(mtcars\(cyl)) # sort( , decreasing =TRUE) - sort in decreasing order sort(table(mtcars\)cyl), decreasing = TRUE) #####################################################
sd(mtcars\(drat) #variance - var() var(mtcars\)qsec) #####################################################
factor(mtcars$cyl)
mtcars\(cyl <- factor(mtcars\)cyl) str(mtcars$cyl) summary(mtcars) ##################################################### # Accessing indexed value - accessing the Mazda RX4 row elements mtcars[‘Mazda RX4’,]
mtcars[,‘hp’]
max <- max(mtcars\(hp) mtcars\)hp == max mtcars[mtcars$hp == max,]
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),])
max <- max(mtcars\(mpg) mtcars\)mpg == max mtcars[mtcars$mpg == max,] #########################################################