R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

##Invoking necessary library

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)

##Printing the structure of dataset

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

##Printing the variables in dataset

names(mtcars)
##  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
## [11] "carb"

##Printing 15 rows from dataset

mtcars %>% slice(1:15)
##                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4          21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag      21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710         22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive     21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant            18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360         14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D          24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230           22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280           19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C          17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE         16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL         17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC        15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4

##User defined function-This function is created to identify whether the car is Manual or Automatic. Here, boolean value is used which means if the value is 1 the car is manual and if tha value is 0, the car is automatic. To invoke the function, we are passing the data of car in 5th row.

cars_type=function(am){
  if (am==1){
    return("Manual")}
  else{
    return("Automatic")
  }
}

cars_type(mtcars$am[5])
## [1] "Automatic"

##Applying filter for sports cars. Here, the assumption made is the criteria for sports car is high hp, low weight and it is manual. After applying the filter we are counting the total number of sports car in our dataset.

mtcars %>%
  filter(`hp` > 150,
         `wt` < 3.5,
         `am` == 1) %>%
  summarise(Count = n())
##   Count
## 1     2

##Identifing the dependent & independent variables and using reshaping techniques(cbind) and creating a new data frame called new_mtcars_df by joining those variables from mtcars dataset.

# Creating variables from the mtcars dataset
mileage = mtcars$mpg      # Dependent variable
cylinders = mtcars$cyl    # Independent
horsepower = mtcars$hp    # Independent
weight = mtcars$wt        # Independent

# Combine into a new data frame
new_mtcars_df = cbind(mileage, cylinders, horsepower, weight)

# View result
new_mtcars_df
##       mileage cylinders horsepower weight
##  [1,]    21.0         6        110  2.620
##  [2,]    21.0         6        110  2.875
##  [3,]    22.8         4         93  2.320
##  [4,]    21.4         6        110  3.215
##  [5,]    18.7         8        175  3.440
##  [6,]    18.1         6        105  3.460
##  [7,]    14.3         8        245  3.570
##  [8,]    24.4         4         62  3.190
##  [9,]    22.8         4         95  3.150
## [10,]    19.2         6        123  3.440
## [11,]    17.8         6        123  3.440
## [12,]    16.4         8        180  4.070
## [13,]    17.3         8        180  3.730
## [14,]    15.2         8        180  3.780
## [15,]    10.4         8        205  5.250
## [16,]    10.4         8        215  5.424
## [17,]    14.7         8        230  5.345
## [18,]    32.4         4         66  2.200
## [19,]    30.4         4         52  1.615
## [20,]    33.9         4         65  1.835
## [21,]    21.5         4         97  2.465
## [22,]    15.5         8        150  3.520
## [23,]    15.2         8        150  3.435
## [24,]    13.3         8        245  3.840
## [25,]    19.2         8        175  3.845
## [26,]    27.3         4         66  1.935
## [27,]    26.0         4         91  2.140
## [28,]    30.4         4        113  1.513
## [29,]    15.8         8        264  3.170
## [30,]    19.7         6        175  2.770
## [31,]    15.0         8        335  3.570
## [32,]    21.4         4        109  2.780

##Searching for missing values

##duplicating dataset for demonstrating removal of missing value as this dataset doesn't contain missing value
new_mtcars=mtcars

##searching for missing values
any(is.na(mtcars))
## [1] FALSE
##adding missing value
mtcars[3, "hp"] = NA
mtcars[5, "mpg"] = NA

##again checking for missing value if it is added
any(is.na(mtcars))
## [1] TRUE
##extracting rows where hp and mpg is NA
new_mtcars %>% filter(is.na(hp))
##  [1] mpg  cyl  disp hp   drat wt   qsec vs   am   gear carb
## <0 rows> (or 0-length row.names)
new_mtcars %>% filter(is.na(mpg))
##  [1] mpg  cyl  disp hp   drat wt   qsec vs   am   gear carb
## <0 rows> (or 0-length row.names)
##removing missing values and storing it in clean_mtcars
clean_mtcars = na.omit(new_mtcars)

##searching for missing values to check if it is removed or not
any(is.na(clean_mtcars))
## [1] FALSE

##Searching for duplicate values

##using new_mtcars for demonstrating of identifying and removal of duplicate data

##checking for duplicate data before beginning
any(duplicated(new_mtcars))
## [1] FALSE
# Adding a duplicate of the first row to demonstrate
new_mtcars = rbind(new_mtcars, new_mtcars[1, ])
new_mtcars = rbind(new_mtcars, new_mtcars[3, ])

##checking for duplicate data to verify if it is added
any(duplicated(new_mtcars))
## [1] TRUE
##removing duplicate rows
clean_mtcars = new_mtcars[!duplicated(new_mtcars), ]

##checking for duplicate data after removing 
any(duplicated(clean_mtcars))
## [1] FALSE

##Arranging rows in descending order

mtcars%>%arrange(desc(disp))
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Hornet Sportabout     NA   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Datsun 710          22.8   4 108.0  NA 3.85 2.320 18.61  1  1    4    1
## 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
## 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
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
mtcars%>%arrange(desc(hp))
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Hornet Sportabout     NA   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Datsun 710          22.8   4 108.0  NA 3.85 2.320 18.61  1  1    4    1
mtcars%>%arrange(desc(drat))
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  NA 3.85 2.320 18.61  1  1    4    1
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Hornet Sportabout     NA   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
mtcars%>%arrange(desc(wt))
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Hornet Sportabout     NA   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Datsun 710          22.8   4 108.0  NA 3.85 2.320 18.61  1  1    4    1
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  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
mtcars%>%arrange(desc(qsec))
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## 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
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Datsun 710          22.8   4 108.0  NA 3.85 2.320 18.61  1  1    4    1
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Hornet Sportabout     NA   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4

##Renaming column names in dataset

names(mtcars)[1]="MPG"
names(mtcars)[2]="Cyl"
names(mtcars)[3]="Disp"
names(mtcars)[4]="Horsepower"
names(mtcars)[5]="DRAT"
names(mtcars)[5]="WT"
names(mtcars)[7]="Qtr_mile_T"
names(mtcars)[8]="Trans"
names(mtcars)[9]="EngineShape"

mtcars
##                      MPG Cyl  Disp Horsepower   WT    wt Qtr_mile_T Trans
## Mazda RX4           21.0   6 160.0        110 3.90 2.620      16.46     0
## Mazda RX4 Wag       21.0   6 160.0        110 3.90 2.875      17.02     0
## Datsun 710          22.8   4 108.0         NA 3.85 2.320      18.61     1
## Hornet 4 Drive      21.4   6 258.0        110 3.08 3.215      19.44     1
## Hornet Sportabout     NA   8 360.0        175 3.15 3.440      17.02     0
## Valiant             18.1   6 225.0        105 2.76 3.460      20.22     1
## Duster 360          14.3   8 360.0        245 3.21 3.570      15.84     0
## Merc 240D           24.4   4 146.7         62 3.69 3.190      20.00     1
## Merc 230            22.8   4 140.8         95 3.92 3.150      22.90     1
## Merc 280            19.2   6 167.6        123 3.92 3.440      18.30     1
## Merc 280C           17.8   6 167.6        123 3.92 3.440      18.90     1
## Merc 450SE          16.4   8 275.8        180 3.07 4.070      17.40     0
## Merc 450SL          17.3   8 275.8        180 3.07 3.730      17.60     0
## Merc 450SLC         15.2   8 275.8        180 3.07 3.780      18.00     0
## Cadillac Fleetwood  10.4   8 472.0        205 2.93 5.250      17.98     0
## Lincoln Continental 10.4   8 460.0        215 3.00 5.424      17.82     0
## Chrysler Imperial   14.7   8 440.0        230 3.23 5.345      17.42     0
## Fiat 128            32.4   4  78.7         66 4.08 2.200      19.47     1
## Honda Civic         30.4   4  75.7         52 4.93 1.615      18.52     1
## Toyota Corolla      33.9   4  71.1         65 4.22 1.835      19.90     1
## Toyota Corona       21.5   4 120.1         97 3.70 2.465      20.01     1
## Dodge Challenger    15.5   8 318.0        150 2.76 3.520      16.87     0
## AMC Javelin         15.2   8 304.0        150 3.15 3.435      17.30     0
## Camaro Z28          13.3   8 350.0        245 3.73 3.840      15.41     0
## Pontiac Firebird    19.2   8 400.0        175 3.08 3.845      17.05     0
## Fiat X1-9           27.3   4  79.0         66 4.08 1.935      18.90     1
## Porsche 914-2       26.0   4 120.3         91 4.43 2.140      16.70     0
## Lotus Europa        30.4   4  95.1        113 3.77 1.513      16.90     1
## Ford Pantera L      15.8   8 351.0        264 4.22 3.170      14.50     0
## Ferrari Dino        19.7   6 145.0        175 3.62 2.770      15.50     0
## Maserati Bora       15.0   8 301.0        335 3.54 3.570      14.60     0
## Volvo 142E          21.4   4 121.0        109 4.11 2.780      18.60     1
##                     EngineShape gear carb
## Mazda RX4                     1    4    4
## Mazda RX4 Wag                 1    4    4
## Datsun 710                    1    4    1
## Hornet 4 Drive                0    3    1
## Hornet Sportabout             0    3    2
## Valiant                       0    3    1
## Duster 360                    0    3    4
## Merc 240D                     0    4    2
## Merc 230                      0    4    2
## Merc 280                      0    4    4
## Merc 280C                     0    4    4
## Merc 450SE                    0    3    3
## Merc 450SL                    0    3    3
## Merc 450SLC                   0    3    3
## Cadillac Fleetwood            0    3    4
## Lincoln Continental           0    3    4
## Chrysler Imperial             0    3    4
## Fiat 128                      1    4    1
## Honda Civic                   1    4    2
## Toyota Corolla                1    4    1
## Toyota Corona                 0    3    1
## Dodge Challenger              0    3    2
## AMC Javelin                   0    3    2
## Camaro Z28                    0    3    4
## Pontiac Firebird              0    3    2
## Fiat X1-9                     1    4    1
## Porsche 914-2                 1    5    2
## Lotus Europa                  1    5    2
## Ford Pantera L                1    5    4
## Ferrari Dino                  1    5    6
## Maserati Bora                 1    5    8
## Volvo 142E                    1    4    2

##Adding new variable as horse power to weight by diving horsepower with weight

mtcars$HPToWT=mtcars$Horsepower/mtcars$WT

mtcars
##                      MPG Cyl  Disp Horsepower   WT    wt Qtr_mile_T Trans
## Mazda RX4           21.0   6 160.0        110 3.90 2.620      16.46     0
## Mazda RX4 Wag       21.0   6 160.0        110 3.90 2.875      17.02     0
## Datsun 710          22.8   4 108.0         NA 3.85 2.320      18.61     1
## Hornet 4 Drive      21.4   6 258.0        110 3.08 3.215      19.44     1
## Hornet Sportabout     NA   8 360.0        175 3.15 3.440      17.02     0
## Valiant             18.1   6 225.0        105 2.76 3.460      20.22     1
## Duster 360          14.3   8 360.0        245 3.21 3.570      15.84     0
## Merc 240D           24.4   4 146.7         62 3.69 3.190      20.00     1
## Merc 230            22.8   4 140.8         95 3.92 3.150      22.90     1
## Merc 280            19.2   6 167.6        123 3.92 3.440      18.30     1
## Merc 280C           17.8   6 167.6        123 3.92 3.440      18.90     1
## Merc 450SE          16.4   8 275.8        180 3.07 4.070      17.40     0
## Merc 450SL          17.3   8 275.8        180 3.07 3.730      17.60     0
## Merc 450SLC         15.2   8 275.8        180 3.07 3.780      18.00     0
## Cadillac Fleetwood  10.4   8 472.0        205 2.93 5.250      17.98     0
## Lincoln Continental 10.4   8 460.0        215 3.00 5.424      17.82     0
## Chrysler Imperial   14.7   8 440.0        230 3.23 5.345      17.42     0
## Fiat 128            32.4   4  78.7         66 4.08 2.200      19.47     1
## Honda Civic         30.4   4  75.7         52 4.93 1.615      18.52     1
## Toyota Corolla      33.9   4  71.1         65 4.22 1.835      19.90     1
## Toyota Corona       21.5   4 120.1         97 3.70 2.465      20.01     1
## Dodge Challenger    15.5   8 318.0        150 2.76 3.520      16.87     0
## AMC Javelin         15.2   8 304.0        150 3.15 3.435      17.30     0
## Camaro Z28          13.3   8 350.0        245 3.73 3.840      15.41     0
## Pontiac Firebird    19.2   8 400.0        175 3.08 3.845      17.05     0
## Fiat X1-9           27.3   4  79.0         66 4.08 1.935      18.90     1
## Porsche 914-2       26.0   4 120.3         91 4.43 2.140      16.70     0
## Lotus Europa        30.4   4  95.1        113 3.77 1.513      16.90     1
## Ford Pantera L      15.8   8 351.0        264 4.22 3.170      14.50     0
## Ferrari Dino        19.7   6 145.0        175 3.62 2.770      15.50     0
## Maserati Bora       15.0   8 301.0        335 3.54 3.570      14.60     0
## Volvo 142E          21.4   4 121.0        109 4.11 2.780      18.60     1
##                     EngineShape gear carb   HPToWT
## Mazda RX4                     1    4    4 28.20513
## Mazda RX4 Wag                 1    4    4 28.20513
## Datsun 710                    1    4    1       NA
## Hornet 4 Drive                0    3    1 35.71429
## Hornet Sportabout             0    3    2 55.55556
## Valiant                       0    3    1 38.04348
## Duster 360                    0    3    4 76.32399
## Merc 240D                     0    4    2 16.80217
## Merc 230                      0    4    2 24.23469
## Merc 280                      0    4    4 31.37755
## Merc 280C                     0    4    4 31.37755
## Merc 450SE                    0    3    3 58.63192
## Merc 450SL                    0    3    3 58.63192
## Merc 450SLC                   0    3    3 58.63192
## Cadillac Fleetwood            0    3    4 69.96587
## Lincoln Continental           0    3    4 71.66667
## Chrysler Imperial             0    3    4 71.20743
## Fiat 128                      1    4    1 16.17647
## Honda Civic                   1    4    2 10.54767
## Toyota Corolla                1    4    1 15.40284
## Toyota Corona                 0    3    1 26.21622
## Dodge Challenger              0    3    2 54.34783
## AMC Javelin                   0    3    2 47.61905
## Camaro Z28                    0    3    4 65.68365
## Pontiac Firebird              0    3    2 56.81818
## Fiat X1-9                     1    4    1 16.17647
## Porsche 914-2                 1    5    2 20.54176
## Lotus Europa                  1    5    2 29.97347
## Ford Pantera L                1    5    4 62.55924
## Ferrari Dino                  1    5    6 48.34254
## Maserati Bora                 1    5    8 94.63277
## Volvo 142E                    1    4    2 26.52068

##Generating random 5 data from the dataset

mtcars %>% sample_n(5, replace = FALSE)
##                      MPG Cyl  Disp Horsepower   WT    wt Qtr_mile_T Trans
## Lincoln Continental 10.4   8 460.0        215 3.00 5.424      17.82     0
## Dodge Challenger    15.5   8 318.0        150 2.76 3.520      16.87     0
## Camaro Z28          13.3   8 350.0        245 3.73 3.840      15.41     0
## Porsche 914-2       26.0   4 120.3         91 4.43 2.140      16.70     0
## Volvo 142E          21.4   4 121.0        109 4.11 2.780      18.60     1
##                     EngineShape gear carb   HPToWT
## Lincoln Continental           0    3    4 71.66667
## Dodge Challenger              0    3    2 54.34783
## Camaro Z28                    0    3    4 65.68365
## Porsche 914-2                 1    5    2 20.54176
## Volvo 142E                    1    4    2 26.52068

##Printing the summary statistics of dataset

summary(mtcars)
##       MPG             Cyl             Disp         Horsepower   
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.35   1st Qu.:4.000   1st Qu.:120.8   1st Qu.:101.0  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.14   Mean   :6.188   Mean   :230.7   Mean   :148.4  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##  NA's   :1                                       NA's   :1      
##        WT              wt          Qtr_mile_T        Trans       
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##                                                                  
##   EngineShape          gear            carb           HPToWT     
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000   Min.   :10.55  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000   1st Qu.:26.37  
##  Median :0.0000   Median :4.000   Median :2.000   Median :38.04  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812   Mean   :43.42  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:58.63  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000   Max.   :94.63  
##                                                   NA's   :1

##Operations on numeric variables from dataset

mean(mtcars$Cyl)
## [1] 6.1875
median(mtcars$Disp)
## [1] 196.3
mode(mtcars$Horsepower)
## [1] "numeric"
range(mtcars$WT)
## [1] 2.76 4.93

Scatter plot for displacement and horsepower

####This scatter plot shows the relationship between engine displacement (Disp) on the x-axis and horsepower on the y-axis. Each point represents one car in the dataset. Key observations:There’s a clear positive correlation between displacement and horsepower. As engine displacement increases, horsepower tends to increase as well. The relationship appears roughly linear with some scatter. Most cars cluster in the lower displacement/horsepower range (around 100-200 displacement, 50-150 horsepower).A few high-performance cars have very high displacement (400+) and horsepower (200-350).

## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).

Bar plot for fuel efficiency and cylinders

##This bar plot shows the distribution of cars by their fuel efficiency (MPG - miles per gallon) on the x-axis, with the count of cars on the y-axis. Key observations:cylinder cars (red) tend to have higher fuel efficiency, mostly clustering around 21-33 MPG,6-cylinder cars (green) have moderate fuel efficiency, around 17-21 MPG, and 8-cylinder cars (blue) generally have lower fuel efficiency, concentrated around 10-17 MPG. ##This demonstrates the expected inverse relationship between number of cylinders and fuel efficiency - more cylinders typically mean lower MPG.

## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_count()`).

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

##co-relation between horsepower and weight

require("datasets")
data("mtcars")
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
Y<- mtcars[,"hp"] # select Target attribute i.e. horsepower
X<- mtcars[,"wt"] # select Predictor attribute i.e. weight
head(X)
## [1] 2.620 2.875 2.320 3.215 3.440 3.460
head(Y)
## [1] 110 110  93 110 175 105
xycorr<- cor(Y,X, method="pearson") # find pearson correlation coefficient
xycorr
## [1] 0.6587479
## To explain the relationship between horsepower and weight, the Pearson correlation coefficient between horsepower (hp) and weight (wt) is approximately 0.66, which indicates a moderate to strong positive linear relationship. This means that as the weight of the car increases, its horsepower also tends to increase. Heavier cars often require more power to perform efficiently, which might explain this relationship.