EXERCISE STA334 19/10

Author

MUHAMMAD ADIB HAKIMI BIN ROSLI

EXERCISE STA334

Exercise 1: ​

  1. Create a data frame named students with the following columns: Name, Age, Grade, and Subject. Populate it with at least 5 rows of sample data.

    student<- data.frame(
      name = c('Ali', "Bob", "Lukman", "Aishah", "Kamal"),
      age = c(10, 20, 19, 28, 31),
      grade = c("A", "A", "B", "B", "C"),
      subject = c("English", "History", "Math", "Economy", "Geometry")
    )
    student
        name age grade  subject
    1    Ali  10     A  English
    2    Bob  20     A  History
    3 Lukman  19     B     Math
    4 Aishah  28     B  Economy
    5  Kamal  31     C Geometry
  2. ​Display the structure of the students data frame using the str() function.

    str(student)
    'data.frame':   5 obs. of  4 variables:
     $ name   : chr  "Ali" "Bob" "Lukman" "Aishah" ...
     $ age    : num  10 20 19 28 31
     $ grade  : chr  "A" "A" "B" "B" ...
     $ subject: chr  "English" "History" "Math" "Economy" ...
  3. ​Add a new column to the students data frame named Attendance and populate it with sample data

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.

  2. ​Retrieve the first 6 rows of the car_specs data frame.

  3. Create a subset of mtcars containing only cars with 6 cylinders (cyl).

​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 column: 0 represents automatic, 1 represents manual)?

  3. ​Which car model in the mtcars dataset has the highest miles per gallon (mpg)?

​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 weigh (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 the names 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).

    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 have more than 100 horsepower (hp) and weigh (column wt) less than 3,000 lbs?

    nrow(mtcars[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" 

GRADE ASSIGNMENT

  1. score <- 87

    if (score >= 90) {

    print(“A”)

    } else if (score >= 80) {

    print(“B”)

    } else if (score >= 70) {

    print(“C”)

    } else if (score >= 60) {

    print(“D”)

    } else {

    print(“FAIL”)

    }

  2. GIVEN SCORE == 87 CLASSIFY SCORE, >=90 A, >=80 - 90 AS B, MPORE THAN 70-80 AS C, 60 - 70 AS D AND ELSE IF FAIL

    score <- 87
    
    if (score >= 90) {
      print("A")
    } else if (score >= 80) {
      print("B")
    } else if (score >= 70) {
      print("C")
    } else if (score >= 60) {
      print("D")
    } else {
      print("FAIL")
    }
    [1] "B"