R Questions & Solutions

Data Frame

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.

    students <- data.frame (Name = c("Alice", "John", "Jane", "Messi", "Noah"),                      Age = c(19,20,21,18,17),
                            Grade = c("A", "B", "C", "D", "E"),
                            Subject = c("Math", "Science", "BM", "English", "PJ"))
    students
    ##    Name Age Grade Subject
    ## 1 Alice  19     A    Math
    ## 2  John  20     B Science
    ## 3  Jane  21     C      BM
    ## 4 Messi  18     D English
    ## 5  Noah  17     E      PJ
  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" "John" "Jane" "Messi" ...
    ##  $ Age    : num  19 20 21 18 17
    ##  $ Grade  : chr  "A" "B" "C" "D" ...
    ##  $ Subject: chr  "Math" "Science" "BM" "English" ...
  3. Add a new column to the students data frame named Attendance and populate it with sample data.

    student2 <- data.frame(Name = c("Alice", "John", "Jane", "Messi", "Noah"),
                           Attendance= c("present", "absent", "present", "absent", "present"))
    student2
    ##    Name Attendance
    ## 1 Alice    present
    ## 2  John     absent
    ## 3  Jane    present
    ## 4 Messi     absent
    ## 5  Noah    present
    merge(students, student2, by= "Name")
    ##    Name Age Grade Subject Attendance
    ## 1 Alice  19     A    Math    present
    ## 2  Jane  21     C      BM    present
    ## 3  John  20     B Science     absent
    ## 4 Messi  18     D English     absent
    ## 5  Noah  17     E      PJ    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.

    cars_specs <- mtcars[ ,c("mpg", "hp")]
    cars_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(cars_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
    summary(mtcars$hp)
    ##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    ##    52.0    96.5   123.0   146.7   180.0   335.0
  2. How many cars in the dataset have an automatic transmission (am column: 0 represents automatic, 1 represents manual)?

    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 ...
    data1 <- mtcars
    
    data1$am <- as.factor(data1$am)
    summary(data1$am)
    ##  0  1 
    ## 19 13
    table(data1$am)
    ## 
    ##  0  1 
    ## 19 13
  3. Which car model in the mtcars dataset has the highest miles per gallon (mpg)?

    summary(mtcars$mpg)
    ##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    ##   10.40   15.43   19.20   20.09   22.80   33.90
    mtcars[mtcars$mpg == 33.90, ]
    ##                 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
    rownames(mtcars)[which.max(mtcars$mpg)]
    ## [1] "Toyota Corolla"

Exercise 4:

  1. Extract and display all cars from mtcars 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
    subset(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 in the mtcars dataset have more than 100 horsepower (hp) and weigh (column wt) less than 3,000 lbs?

    mtcars[mtcars$hp > 100 & mtcars$wt < 3, ]
    ##                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
    ## Lotus Europa  30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
    ## Ferrari Dino  19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
    ## Volvo 142E    21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
    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?

    nrow(mtcars)
    ## [1] 32
    ncol(mtcars)
    ## [1] 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, n=10)
    ##                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
    ## AMC Javelin      15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
    ## Camaro Z28       13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
    ## 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)?

    data1 <- mtcars
    
    data1$am <- as.factor(data1$am)
    summary(data1$am)
    ##  0  1 
    ## 19 13
    table(data1$am)
    ## 
    ##  0  1 
    ## 19 13
  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
    subset(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 weigh (column wt) less than 3,000 lbs?

    mtcars[mtcars$hp > 100 & mtcars$wt < 3, ]
    ##                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
    ## Lotus Europa  30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
    ## Ferrari Dino  19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
    ## Volvo 142E    21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
    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"