Exercise 2

Exercise 2

Exercise 1:

  1. Create a data frame named students with the following columns, Name, Age, Grade,

    and Subject. Populate it with atleast 5 rows of sample data.

    students <- data.frame(
      Name = c("Alice","Bob","Lukman","Aishah","Kamal"),
      Age = c(10,20,19,28,21),
      Grade = as.factor(c("A","A","B","A","C")),
      Subject = as.factor(c("Math","History","Math","English","History"))
    )
    
    students
        Name Age Grade Subject
    1  Alice  10     A    Math
    2    Bob  20     A History
    3 Lukman  19     B    Math
    4 Aishah  28     A English
    5  Kamal  21     C History
  2. Display the structure of the students data frame using the str() function.

    str(students)
    'data.frame':   5 obs. of  4 variables:
     $ Name   : chr  "Alice" "Bob" "Lukman" "Aishah" ...
     $ Age    : num  10 20 19 28 21
     $ Grade  : Factor w/ 3 levels "A","B","C": 1 1 2 1 3
     $ Subject: Factor w/ 3 levels "English","History",..: 3 2 3 1 2
  3. Add a new column to the students data frame named Attendance and populate

    it with sample data.

    students$Attendance <- c("Present", "Absent", "Present", "Present", "Absent")
    
    students
        Name Age Grade Subject Attendance
    1  Alice  10     A    Math    Present
    2    Bob  20     A History     Absent
    3 Lukman  19     B    Math    Present
    4 Aishah  28     A English    Present
    5  Kamal  21     C History     Absent

Exercise 2:

  1. From the mtcars dataset, extract the mpg(miles per gallon) and hp (horsepower)

    columns and save them as a new data frame named car_specs.

    car_specs <- mtcars[ , c("mpg","hp")]
  2. Retrieve the first 6 rows of the car_specs data frame.

    head(car_specs)
                       mpg  hp
    Mazda RX4         21.0 110
    Mazda RX4 Wag     21.0 110
    Datsun 710        22.8  93
    Hornet 4 Drive    21.4 110
    Hornet Sportabout 18.7 175
    Valiant           18.1 105
  3. Create a subset of mtcars containing only cars with 6 cylinders (cyl).

    six_cyl_cars <- mtcars[mtcars$cyl == 6, ]
    six_cyl_cars
                    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
    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
    Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6

Exercise 3:

  1. Calculate the median horsepower (hp) of all cars in the mtcars dataset

    median(mtcars$hp)
    [1] 123
  2. How many cars in the dataset have an automatic transmission (am coumn: 0 represent

    automatic, 1 represents manual)?

    nrow(mtcars[mtcars$am == 0, ])
    [1] 19
  3. Which car model in the mtcars dataset has the highest miles per gallon (mpg)?

    rownames(mtcars)[which.max(mtcars$mpg)]
    [1] "Toyota Corolla"

Exercise 4:

  1. Extract and display all cars from mtcars with 4 cylinders (cyl).

    rownames(mtcars[mtcars$cyl == 4, ])
     [1] "Datsun 710"     "Merc 240D"      "Merc 230"       "Fiat 128"      
     [5] "Honda Civic"    "Toyota Corolla" "Toyota Corona"  "Fiat X1-9"     
     [9] "Porsche 914-2"  "Lotus Europa"   "Volvo 142E"    
  2. How many cars in the mtcars dataset have more than 100 horsepower (hp) and

    weight (column wt) less than 3,000 lbs?

    nrow(mtcars[mtcars$hp > 100 & mtcars$wt < 3, ])
    [1] 5
  3. Retrieve all car models from mtcars that have an automatic transmission and can

    cover more than 20 miles per gallon.

    rownames(mtcars[mtcars$am == 0 & mtcars$mpg > 20, ])
    [1] "Hornet 4 Drive" "Merc 240D"      "Merc 230"       "Toyota Corona" 

Exercise 5:

  1. How many rows and columns are present in the mtcars dataset?

    dim(mtcars)
    [1] 32 11
  2. What are names all of all the columns in the dataset?

    colnames(mtcars)
     [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
    [11] "carb"
  3. Display the last 8 rows of the dataset.

    tail(mtcars, 8)
                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
    Pontiac Firebird 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    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
    Lotus Europa     30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
    Ford Pantera L   15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
    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
    Volvo 142E       21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

Exercise 6:

  1. Calculate the median horsepower (hp) of all cars in the dataset.

    median(mtcars$hp)
    [1] 123
  2. How many cars in the dataset have an automatic transmission (am column: 0 represents

    automatic, 1 represents manual)?

    sum(mtcars$am == 0)
    [1] 19
  3. Which car model has the highest miles per gallon (mpg)?

    rownames(mtcars)[which.max(mtcars$mpg)]
    [1] "Toyota Corolla"

Exercise 7:

  1. Extract and display all cars with 4 cylinders (cyl).

    mtcars[mtcars$cyl == 4, ]
                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
    Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
    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
    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
    Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
    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
    Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
    Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
  2. How many cars have more than 100 horsepower (hp) and weight (column wt) less than

    3,000 lbs?

    sum(mtcars$hp > 100 & mtcars$wt < 3)
    [1] 5
  3. Retrieve all car models that have an automatic transmission and can cover more than 20 miles

    per gallon.

    rownames(mtcars[mtcars$am == 0 & mtcars$mpg > 20, ])
    [1] "Hornet 4 Drive" "Merc 240D"      "Merc 230"       "Toyota Corona"