[This article was first published on Data Science Using R – FinderDing, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

## Using Basic Data Analysis functions on the mtcars dataset

### Let’s Start

# Copying mtcars data frame to our new data frame myCars
myCars <- mtcars

#find and display the car with the highest horsepower index <- which.max(myCars$hp) # Display the car name along with the rest of the row myCars[index,] ## mpg cyl disp hp drat wt qsec vs am gear carb ## Maserati Bora 15 8 301 335 3.54 3.57 14.6 0 1 5 8 Maserati Bora has the highest horsepower at 335 #### Exploring miles per gallon (mpg) of the cars # find and display the car with the highest mpg index<-which.max(myCars$mpg)
myCars[index,]
##                 mpg cyl disp hp drat    wt qsec vs am gear carb ## Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.9  1  1    4    1
# Creating a sorted dataframe, based on mpg
highMPGcars <- myCars[ order(-myCars$mpg),] head(highMPGcars) mpg cyl disp hp drat wt qsec vs am gear carb ## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 ## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 ## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 ## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 ## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 ## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 #### Which car has the “best” combination of mpg and hp? # Best car combination of mpg and hp, where mpg and hp must be given equal # weight bestCombo<- myCars$hp / myCars\$mpg
myCars[which.max(bestCombo),]
##                mpg cyl disp  hp drat   wt qsec vs am gear carb ## Maserati Bora  15   8  301 335 3.54 3.57 14.6  0  1    5    8

The Maserati Bora hp to mpg is ~ 22hp per gallon

The post Introduction to Data Analysis with R appeared first on FinderDing.