EXERCISE sir

Author

MUHAMMAD khairul

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("Math", "History", "Math", "English", "History")
    )
    student
        name age grade subject
    1    Ali  10     A    Math
    2    Bob  20     A History
    3 Lukman  19     B    Math
    4 Aishah  28     B English
    5  Kamal  31     C History
  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  "Math" "History" "Math" "English" ...
  3. ​Add a new column to the students data frame named Attendance and populate it with sample data

    student$Attendance<- as.factor(c("Present", "Present", "Absent", "Absent", "Present"))
    student$Attendance
    [1] Present Present Absent  Absent  Present
    Levels: Absent Present

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")]
    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
    Duster 360          14.3 245
    Merc 240D           24.4  62
    Merc 230            22.8  95
    Merc 280            19.2 123
    Merc 280C           17.8 123
    Merc 450SE          16.4 180
    Merc 450SL          17.3 180
    Merc 450SLC         15.2 180
    Cadillac Fleetwood  10.4 205
    Lincoln Continental 10.4 215
    Chrysler Imperial   14.7 230
    Fiat 128            32.4  66
    Honda Civic         30.4  52
    Toyota Corolla      33.9  65
    Toyota Corona       21.5  97
    Dodge Challenger    15.5 150
    AMC Javelin         15.2 150
    Camaro Z28          13.3 245
    Pontiac Firebird    19.2 175
    Fiat X1-9           27.3  66
    Porsche 914-2       26.0  91
    Lotus Europa        30.4 113
    Ford Pantera L      15.8 264
    Ferrari Dino        19.7 175
    Maserati Bora       15.0 335
    Volvo 142E          21.4 109
  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).

    mtcars[mtcars$cyl==6, ]
                    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 column: 0 represents automatic, 1 represents manual)?

    length(mtcars$am[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 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"