Introduction to R - PS 2



1. Practice: ordering, subsetting, appending. Order the mtcars data frame by ascending number of carburetors and weight, create two data frames with the top 3 and bottom 3 rows according to this order, append them both.

  attach(mtcars)
  mtcars
  ##                      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
  ## 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
  ## 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
  ## 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
  ## 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
  # order dataset by wt and carb - first define the index of sorted rows & then use the sorted order conditions
  mysort <- order(mtcars$carb, mtcars$wt, decreasing = FALSE)
  mtcars[mysort, ]
  ##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
  ## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
  ## 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
  ## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
  ## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
  ## 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
  ## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
  ## 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
  ## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
  ## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
  ## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
  ## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
  ## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
  ## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
  ## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
  ## 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
  ## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
  ## 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
  ## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
  ## 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
  ## 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
  ## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  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
  ## 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
  # create the new 2 datasets and append them; we'll bind rows; we could also use smartbind() for instance if 
  # variable names wouldn't be the same
  mtcarsTop3 <- head(mtcars[mysort, ], 3)
  mtcarsTop3
  ##                 mpg cyl disp hp drat    wt  qsec vs am gear carb
  ## Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.90  1  1    4    1
  ## 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
  mtcarsBottom3 <- tail(mtcars[mysort, ], 3)
  mtcarsBottom3
  ##                      mpg cyl disp  hp drat    wt  qsec vs am gear carb
  ## Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
  ## Ferrari Dino        19.7   6  145 175 3.62 2.770 15.50  0  1    5    6
  ## Maserati Bora       15.0   8  301 335 3.54 3.570 14.60  0  1    5    8
  myNewMtcars <- rbind(mtcarsTop3, mtcarsBottom3)
  myNewMtcars
  ##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
  ## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
  ## 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
  ## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    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
  detach(mtcars)




2. Practice: joining grouped summaries. Add the mean horsepower for each cylinder type to the mtcars data. Hint: first create a data frame with the grouped means (for example using the aggregate() function), then merge it with the mtcars data frame.

1:nrow row is not kept - i.e. car brands


attach(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 ...
#- mean horsepower grouped by the number of cylinders
dataAggregation <- aggregate(mtcars$hp, by=list(cyl), FUN=mean, na.rm=TRUE)
dataAggregation
##   Group.1      x
## 1       4  82.64
## 2       6 122.29
## 3       8 209.21
# rename programmatically 
library(reshape)    #install.packages("reshape")
dataAggregation <- rename(dataAggregation, c(x="meanHp"))
dataAggregation <- rename(dataAggregation, c(Group.1="cyl"))
dataAggregation
##   cyl meanHp
## 1   4  82.64
## 2   6 122.29
## 3   8 209.21
mtcarsMerge <- merge(mtcars, dataAggregation, by = "cyl")
mtcarsMerge
##    cyl  mpg  disp  hp drat    wt  qsec vs am gear carb meanHp
## 1    4 22.8 140.8  95 3.92 3.150 22.90  1  0    4    2  82.64
## 2    4 22.8 108.0  93 3.85 2.320 18.61  1  1    4    1  82.64
## 3    4 24.4 146.7  62 3.69 3.190 20.00  1  0    4    2  82.64
## 4    4 21.5 120.1  97 3.70 2.465 20.01  1  0    3    1  82.64
## 5    4 30.4  75.7  52 4.93 1.615 18.52  1  1    4    2  82.64
## 6    4 33.9  71.1  65 4.22 1.835 19.90  1  1    4    1  82.64
## 7    4 26.0 120.3  91 4.43 2.140 16.70  0  1    5    2  82.64
## 8    4 30.4  95.1 113 3.77 1.513 16.90  1  1    5    2  82.64
## 9    4 32.4  78.7  66 4.08 2.200 19.47  1  1    4    1  82.64
## 10   4 21.4 121.0 109 4.11 2.780 18.60  1  1    4    2  82.64
## 11   4 27.3  79.0  66 4.08 1.935 18.90  1  1    4    1  82.64
## 12   6 21.0 160.0 110 3.90 2.620 16.46  0  1    4    4 122.29
## 13   6 21.0 160.0 110 3.90 2.875 17.02  0  1    4    4 122.29
## 14   6 17.8 167.6 123 3.92 3.440 18.90  1  0    4    4 122.29
## 15   6 21.4 258.0 110 3.08 3.215 19.44  1  0    3    1 122.29
## 16   6 18.1 225.0 105 2.76 3.460 20.22  1  0    3    1 122.29
## 17   6 19.2 167.6 123 3.92 3.440 18.30  1  0    4    4 122.29
## 18   6 19.7 145.0 175 3.62 2.770 15.50  0  1    5    6 122.29
## 19   8 18.7 360.0 175 3.15 3.440 17.02  0  0    3    2 209.21
## 20   8 17.3 275.8 180 3.07 3.730 17.60  0  0    3    3 209.21
## 21   8 14.3 360.0 245 3.21 3.570 15.84  0  0    3    4 209.21
## 22   8 14.7 440.0 230 3.23 5.345 17.42  0  0    3    4 209.21
## 23   8 10.4 472.0 205 2.93 5.250 17.98  0  0    3    4 209.21
## 24   8 16.4 275.8 180 3.07 4.070 17.40  0  0    3    3 209.21
## 25   8 19.2 400.0 175 3.08 3.845 17.05  0  0    3    2 209.21
## 26   8 15.2 275.8 180 3.07 3.780 18.00  0  0    3    3 209.21
## 27   8 15.2 304.0 150 3.15 3.435 17.30  0  0    3    2 209.21
## 28   8 10.4 460.0 215 3.00 5.424 17.82  0  0    3    4 209.21
## 29   8 15.8 351.0 264 4.22 3.170 14.50  0  1    5    4 209.21
## 30   8 15.5 318.0 150 2.76 3.520 16.87  0  0    3    2 209.21
## 31   8 15.0 301.0 335 3.54 3.570 14.60  0  1    5    8 209.21
## 32   8 13.3 350.0 245 3.73 3.840 15.41  0  0    3    4 209.21
detach(mtcars)




3.Practice: binning into multiple categories with cut(). Bin horsepower (from the mtcarsBis dataset) into 4 categories using the cut() function. Add a new column to the dataset with this categorical values.

# Replicate the data frame (for keeping the # original data unchanged)
mtcarsBis <- mtcars

mtcarsBis$myCut1 <- cut(mtcarsBis$hp, breaks = 4)
mtcarsBis$myCut2 <- cut(mtcarsBis$hp, breaks = 4, labels=c(1:4))
mtcarsBis
##                      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
## 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
## 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
## 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
## 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
##                         myCut1 myCut2
## Mazda RX4           (51.7,123]      1
## Mazda RX4 Wag       (51.7,123]      1
## Datsun 710          (51.7,123]      1
## Hornet 4 Drive      (51.7,123]      1
## Hornet Sportabout    (123,194]      2
## Valiant             (51.7,123]      1
## Duster 360           (194,264]      3
## Merc 240D           (51.7,123]      1
## Merc 230            (51.7,123]      1
## Merc 280             (123,194]      2
## Merc 280C            (123,194]      2
## Merc 450SE           (123,194]      2
## Merc 450SL           (123,194]      2
## Merc 450SLC          (123,194]      2
## Cadillac Fleetwood   (194,264]      3
## Lincoln Continental  (194,264]      3
## Chrysler Imperial    (194,264]      3
## Fiat 128            (51.7,123]      1
## Honda Civic         (51.7,123]      1
## Toyota Corolla      (51.7,123]      1
## Toyota Corona       (51.7,123]      1
## Dodge Challenger     (123,194]      2
## AMC Javelin          (123,194]      2
## Camaro Z28           (194,264]      3
## Pontiac Firebird     (123,194]      2
## Fiat X1-9           (51.7,123]      1
## Porsche 914-2       (51.7,123]      1
## Lotus Europa        (51.7,123]      1
## Ford Pantera L       (194,264]      3
## Ferrari Dino         (123,194]      2
## Maserati Bora        (264,335]      4
## Volvo 142E          (51.7,123]      1

Here the function cut() takes in as the first argument the continuous variable mtcarsBis$hp and it cuts it into 4 intervals. By default, the right side of the interval is closed while the left is open. In order to add our own labels instead of the default intervals, just add the labels=c(1:4) option to the cut() function.




4.More on the apply() functions. Apply the fivenum() function to
1) all the columns of mtcars, customizing the output row names;
2) the weight, grouping by number of carburetors.

# Replicate the data frame (for keeping the # original data unchanged)
mtcarsBis <- mtcars

tabel <- apply(mtcars, 2,fivenum)

id <- c("Min","Q1","Med","Q3","Max") 
tabel <- cbind(id, tabel)
tabel
##      id    mpg     cyl disp     hp    drat    wt       qsec     vs  am 
## [1,] "Min" "10.4"  "4" "71.1"   "52"  "2.76"  "1.513"  "14.5"   "0" "0"
## [2,] "Q1"  "15.35" "4" "120.65" "96"  "3.08"  "2.5425" "16.885" "0" "0"
## [3,] "Med" "19.2"  "6" "196.3"  "123" "3.695" "3.325"  "17.71"  "0" "0"
## [4,] "Q3"  "22.8"  "8" "334"    "180" "3.92"  "3.65"   "18.9"   "1" "1"
## [5,] "Max" "33.9"  "8" "472"    "335" "4.93"  "5.424"  "22.9"   "1" "1"
##      gear carb
## [1,] "3"  "1" 
## [2,] "3"  "2" 
## [3,] "4"  "2" 
## [4,] "4"  "4" 
## [5,] "5"  "8"
# to wt by carburators
tapply(mtcars$wt, mtcars$carb, fivenum)
## $`1`
## [1] 1.835 2.067 2.320 2.840 3.460
## 
## $`2`
## [1] 1.513 2.140 3.170 3.440 3.845
## 
## $`3`
## [1] 3.730 3.755 3.780 3.925 4.070
## 
## $`4`
## [1] 2.620 3.170 3.505 5.250 5.424
## 
## $`6`
## [1] 2.77 2.77 2.77 2.77 2.77
## 
## $`8`
## [1] 3.57 3.57 3.57 3.57 3.57




5. Practice: chaining multiple if..else conditions. Replicate the practice example in the Variable transformations section using if..else clauses: bin horsepower (from the mtcarsBis dataset) into 4 categories using conditional expressions (instead of the cut() function).

# Replicate the data frame (for keeping the # original data unchanged)
mtcarsBis <- mtcars

mtcarsBis$myCut3 <- with(mtcarsBis, ifelse(hp <= 123, 1, 
  ifelse(hp > 123 & hp <= 194, 2, 
       ifelse(hp > 194 & hp <= 264, 3,4)))
)

mtcarsBis
##                      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
## 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
## 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
## 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
## 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
##                     myCut3
## Mazda RX4                1
## Mazda RX4 Wag            1
## Datsun 710               1
## Hornet 4 Drive           1
## Hornet Sportabout        2
## Valiant                  1
## Duster 360               3
## Merc 240D                1
## Merc 230                 1
## Merc 280                 1
## Merc 280C                1
## Merc 450SE               2
## Merc 450SL               2
## Merc 450SLC              2
## Cadillac Fleetwood       3
## Lincoln Continental      3
## Chrysler Imperial        3
## Fiat 128                 1
## Honda Civic              1
## Toyota Corolla           1
## Toyota Corona            1
## Dodge Challenger         2
## AMC Javelin              2
## Camaro Z28               3
## Pontiac Firebird         2
## Fiat X1-9                1
## Porsche 914-2            1
## Lotus Europa             1
## Ford Pantera L           3
## Ferrari Dino             2
## Maserati Bora            4
## Volvo 142E               1
#if (any(mtcarsBis$hp <= 123)) {mtcarsBis$myCut3<-1} 
  # else if (any(mtcarsBis$hp>123 & mtcarsBis$hp<=194)) {mtcarsBis$myCut3<-2} 
   #       else if (any(mtcarsBis$hp>194 & mtcarsBis$hp<=264)) { mtcarsBis$myCut3=3} 
    #           else if (any(mtcarsBis$hp>264)){ mtcarsBis$myCut3=4}




6. Practice: looping through data frame columns. Obtain the average of each column in the mtcars dataset looping through each of its columns with a for statement. Store the results in a single vector. Hint: identify each column with indices, not column names, and use the symbol in the loop condition as the column index.

# Replicate the data frame (for keeping the # original data unchanged)
mtcarsBis <- mtcars

x <- 1:11


varlist <- c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb")
match(varlist, colnames(mtcarsBis))
##  [1]  1  2  3  4  5  6  7  8  9 10 11
for( i in match(varlist, colnames(mtcarsBis))){

x[i] <- (mean(mtcarsBis[[i]] ))
}

x
##  [1]  20.0906   6.1875 230.7219 146.6875   3.5966   3.2172  17.8487
##  [8]   0.4375   0.4062   3.6875   2.8125




7. Practice: loop functions. Replicate the seq() function with by as parameter. Check it with the parameters from=1, to=10, by=3 (it should output a vector with these elements: 1, 4, 7, 10).

seqReplicated2 <- function(from, to, by) { 
  sequence <- from # initialize the vector
  while (max(sequence) < to - 1) { 
    sequence <- c(sequence, sequence[length(sequence)] + by) 
} 
return(sequence) 
}

(result <- seqReplicated2(1, 10,3)) 
## [1]  1  4  7 10




8. Practice: sourcing a custom function. Choose some of the replicas of the seq() function we wrote above and store it as a single script (save as seqReplica.R). Use this function in a new code by first sourcing the seqReplica.R.

source("seqReplica.R")
(result <- seqReplicated2(1, 10,3)) 
## [1]  1  4  7 10