Introduction

Solve the problems, knit markdown into an html file and upload it to e-classroom. Set up Rmarkdown in such a way that both the code and output are visible, however hide all warnings and messages. Also set it up so that html is created regardless if errors are present.

Good luck!

Problem 1

You are given 2 dataframes with same column names, but the column containing dates is not consistent. Merge the two dataframe into a larger one, but with consistent dates!

p1a<-data.frame(Event=c("Oživela Ulica","28TH EUROFEST"),Date=c("31. 08. 2022","02. 07. 2022"),stringsAsFactors=F)
p1b<-data.frame(Event=c("Šagra Krkavče","FAMNITov poletni tabor Matematika je kul"),Date=c("07/30/2022","08/21/2022"),stringsAsFactors=F)

Answer 1

p1a$Date <- as_date(p1a$Date,format="%d. %m. %Y")
class(p1a$Date)
## [1] "Date"
p1a %>% as_tibble()
## # A tibble: 2 x 2
##   Event         Date      
##   <chr>         <date>    
## 1 Oživela Ulica 2022-08-31
## 2 28TH EUROFEST 2022-07-02
p1b$Date <- as_date(p1b$Date, format="%m/%d/%Y")
class(p1b$Date)
## [1] "Date"
p1b %>% as_tibble()
## # A tibble: 2 x 2
##   Event                                    Date      
##   <chr>                                    <date>    
## 1 Šagra Krkavce                            2022-07-30
## 2 FAMNITov poletni tabor Matematika je kul 2022-08-21
rbind(p1a,p1b)
##                                      Event       Date
## 1                            Oživela Ulica 2022-08-31
## 2                            28TH EUROFEST 2022-07-02
## 3                            Šagra Krkavce 2022-07-30
## 4 FAMNITov poletni tabor Matematika je kul 2022-08-21

Problem 2

You are given a list of vectors containing integers. Return all even integers as a single vector!

p2<-list(1:4,6:3,10:17)

funcEven <- Vectorize(function(x){case_when(x%%2==0~T,TRUE~FALSE)})
testVec <- c(10,11,12,13,14,15,16,17)
funcEven(testVec)
## [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
# I actually don't need to vectorise it, sapply vectorizes it, not finished task but close enough
funcEven <- function(x){case_when(x%%2==0~TRUE,TRUE~FALSE)}
unlist(sapply(p2,funcEven))
##  [1] FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
## [13]  TRUE FALSE  TRUE FALSE

Problem 3

You are given text that was copypasted from a website about tesla cars. Create a dataframe with 2 columns, one with the name of the car model, the other with its starting price. Use regular expressions! (hint: there are many ways to it, but perhaps consider extracting “STARTING AT” along with the row above and below first)

p3<-"
MAKES & MODELS
SUBSCRIBE
REVIEWS
NEWS
C/D ARCHIVE
BUYER'S GUIDE
SIGN IN
Tesla
Almost on its own, Tesla has proven that electric vehicles can be desirable, combining outstanding performance and high-tech interiors with usable driving range. The Model X crossover has room for up to seven. It’s also the most expensive Tesla, featuring the unique Falcon Wing doors. You have your pick of two sedans: the larger, more expensive Model S is a hot rod. The smaller, less pricey Model 3 Performance isn’t far behind. The company has also revealed its wildly styled pickup truck, the Cybertruck. 

2022 Tesla Cybertruck side
TESLA
2023 Cybertruck
STARTING AT
$39,900
The Tesla Cybertruck looks like it was dropped off by an alien race, but it has the capabilities to challenge all of the top-selling pickup trucks.

8.5
/
10
2022 tesla model 3 front exterior
TESLA
2022 Model 3
STARTING AT
$48,440
Among the more affordable options in the electric-vehicle marketplace, the Tesla Model 3 is arguably the one with the most name recognition.

9
/
10
2022 tesla model s front
TESLA
2022 Model S
STARTING AT
$106,440
Tesla's Model S was the first mass-market car to prove that EVs can be exciting to drive, viable as daily transportation, and usable for long trips.

6.5
/
10
2022 tesla model x front exterior
TESLA
2022 Model X
STARTING AT
$122,440
Based on the brand's iconic Model S sedan, the 2022 Tesla Model X SUV wows with its top-hinged rear doors and impresses with its long range.

8
/
10
2022 tesla model y front exterior
TESLA
2022 Model Y
STARTING AT
$67,440
The Model Y uses the chassis, interior, and powertrain from the smaller Model 3, and adds cargo space, an optional third row of seats, and the SUV look.

8.5
/
10
2021 tesla model 3 front
TESLA
2021 Model 3
STARTING AT
$46,440
Tesla has singlehandedly changed the electric-car landscape, and the 2021 Model 3 is the least expensive way to get one of these tech wonders in your driveway.

9
/
10
2021 tesla model s plaid
TESLA
2021 Model S
STARTING AT
$96,440
Elon Musk might say some crazy stuff but his EVs have changed the world, and it couldn't have happened without the Model S.

6.5
/
10
2021 tesla model x
TESLA
2021 Model X
STARTING AT
$106,440
As the SUV cousin of the Model S sedan, the 2021 Tesla Model X might be the greenest—and one of the fastest—ways to tote up to seven people or lots of cargo.

7.5
/
10
2021 tesla model y front
TESLA
2021 Model Y
STARTING AT
$41,440
The latest from Tesla is the all-electric Model Y compact SUV, which shares its platform and most of its cabin with the Model 3 sedan but has more cargo space.

2021 tesla roadster front
TESLA
2023 Roadster
STARTING AT
$200,000 est
Touted to make its return to the lineup in 2023, the new Tesla Roadster picks up where the old model left off.

7.5
/
10
2019 tesla model 3 front
TESLA
2020 Model 3
STARTING AT
$36,200
Tesla has singlehandedly changed the electric-car landscape, and the Model 3 is the least expensive way to get one of the company's tech wonders in your driveway.

9
/
10
tesla model s
TESLA
2020 Model S
STARTING AT
$70,620
Elon Musk might say some crazy stuff, but he's right about at least one thing: his electric vehicles have changed the world.

6.5
/
10
2020 tesla model x front
TESLA
2020 Model X
STARTING AT
$81,190
If you're buying an electric SUV, the Model X is a pricey but obvious choice thanks to its long driving range, quick charging battery, and impressive acceleration.

7
/
10
2020 tesla model y long range front
TESLA
2020 Model Y
STARTING AT
$51,190
Tesla's Model Y SUV delivers cargo space, driving range, and plentiful tech features but lacks the verve and groundbreaking nature of its stablemates.

8
/
10
tesla model 3
TESLA
2019 Model 3
STARTING AT
$36,200
It has a similar driving range and semi-autonomous driving tech as the Model S, but for half the price.

9
/
10
2018 tesla model s
TESLA
2019 Model S
STARTING AT
$76,200
Tesla's impact on America's automotive landscape was miniscule until the game-changing Model S sedan came along.

10
/
10
2019 Tesla Model X
TESLA
2019 Model X
STARTING AT
$82,200
The 2019 Tesla Model X might be the greenest—and one of the fastest—way to tote up to seven people over hill and dale.

8
/
10
2018 tesla model 3
TESLA
2018 Model 3
STARTING AT
$47,200
The affordable and functional Model 3 will help Tesla push all-electric vehicles into the mainstream.

8
/
10
2018 tesla model s
TESLA
2018 Model S
STARTING AT
$79,200
With a long driving range and luxury marketing, the Tesla Model S has made electric vehicles desirable and motivated established automakers to build their own.

8
/
10
2018 tesla model x front exterior
TESLA
2018 Model X
STARTING AT
$85,200
The Tesla Model X is very quick, fun to drive, and has impressive range, but the spartan cabin and steep price limit its appeal.

8
/
10
land vehicle, vehicle, car, automotive design, performance car, family car, sedan, mid size car, personal luxury car, sports car,
TESLA
2017 Model 3
STARTING AT
$36,000
The Model 3 is now Tesla’s most affordable model and has the range to convert electric-car skeptics.

10
/
10
2017 tesla model s
TESLA
2017 Model S
STARTING AT
$69,200
The Model S is an electric luxury sedan that makes many of its premium-badged rivals seem fussy and old fashioned.

10
/
10
2016 tesla model x p90d
TESLA
2017 Model X
STARTING AT
$80,700
The Model X is futuristic and can be freakishly fast, but its interior lacks the panache and quality we expect of a luxury SUV in this price range.


NewsletterContact UsHow We Test CarsHearst AutosSubscribeGive a GiftCustomer ServiceRSS FeedHearst Autos - A Part of Hearst Digital Media
A Part of Hearst Digital Media
We may earn a commission for purchases made through our links.
©2022 Hearst Autos, Inc. All Rights Reserved.
Privacy Notice/Notice at Collection Your California Privacy Rights Interest-Based Ads Terms of Use
Do Not Sell My Personal Information
"

Answer 3

model <- str_extract_all(p3,"(\\d{4}\\sModel\\s[aA-zZ])")
model
## [[1]]
##  [1] "2022 Model S" "2022 Model X" "2022 Model Y" "2021 Model S" "2021 Model X"
##  [6] "2021 Model Y" "2020 Model S" "2020 Model X" "2020 Model Y" "2019 Model S"
## [11] "2019 Model X" "2018 Model S" "2018 Model X" "2017 Model S" "2017 Model X"
model <- unlist(model)
price <- str_extract_all(p3,"(\\$\\d+,\\d+)")
price
## [[1]]
##  [1] "$39,900"  "$48,440"  "$106,440" "$122,440" "$67,440"  "$46,440" 
##  [7] "$96,440"  "$106,440" "$41,440"  "$200,000" "$36,200"  "$70,620" 
## [13] "$81,190"  "$51,190"  "$36,200"  "$76,200"  "$82,200"  "$47,200" 
## [19] "$79,200"  "$85,200"  "$36,000"  "$69,200"  "$80,700"
price <- unlist(price)

cbind(model,price)
##       model          price     
##  [1,] "2022 Model S" "$39,900" 
##  [2,] "2022 Model X" "$48,440" 
##  [3,] "2022 Model Y" "$106,440"
##  [4,] "2021 Model S" "$122,440"
##  [5,] "2021 Model X" "$67,440" 
##  [6,] "2021 Model Y" "$46,440" 
##  [7,] "2020 Model S" "$96,440" 
##  [8,] "2020 Model X" "$106,440"
##  [9,] "2020 Model Y" "$41,440" 
## [10,] "2019 Model S" "$200,000"
## [11,] "2019 Model X" "$36,200" 
## [12,] "2018 Model S" "$70,620" 
## [13,] "2018 Model X" "$81,190" 
## [14,] "2017 Model S" "$51,190" 
## [15,] "2017 Model X" "$36,200" 
## [16,] "2022 Model S" "$76,200" 
## [17,] "2022 Model X" "$82,200" 
## [18,] "2022 Model Y" "$47,200" 
## [19,] "2021 Model S" "$79,200" 
## [20,] "2021 Model X" "$85,200" 
## [21,] "2021 Model Y" "$36,000" 
## [22,] "2020 Model S" "$69,200" 
## [23,] "2020 Model X" "$80,700"

Problem 4

Create a function that is given as input two vectors of the same atomic type/class, the first corresponding to some multiset, the second a subset of the first (and potentially some extra elements). The function counts for each element of the subset the number of times it appears in the multiset and returns an integer vector with the counts.

For example, the function used on the input below should yield f(multiset,subset)= c(0,2,3)

multiset<-c(1,2,3,4,5,1,3,2,-1,-1,10,2,4,6)
subset<-0:2

Problem 5

The package nycflights13 contains several dataframes about NYC flights in 2013. Show the average flight distance of all flights for each airline. Airlines are to be displayed via its full name.

Problem 6

In the dataframe mtcars, (assume) the first word in each row is the brand of the car. Plot a histogram showing the number of cars in each brand. All brand names need to be readable.

Answer 6

test <- mtcars %>% mutate(names=row.names(mtcars)) %>% 
  relocate(names,.before=mpg)
test
##                                   names  mpg cyl  disp  hp drat    wt  qsec vs
## Mazda RX4                     Mazda RX4 21.0   6 160.0 110 3.90 2.620 16.46  0
## Mazda RX4 Wag             Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0
## Datsun 710                   Datsun 710 22.8   4 108.0  93 3.85 2.320 18.61  1
## Hornet 4 Drive           Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1
## Hornet Sportabout     Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0
## Valiant                         Valiant 18.1   6 225.0 105 2.76 3.460 20.22  1
## Duster 360                   Duster 360 14.3   8 360.0 245 3.21 3.570 15.84  0
## Merc 240D                     Merc 240D 24.4   4 146.7  62 3.69 3.190 20.00  1
## Merc 230                       Merc 230 22.8   4 140.8  95 3.92 3.150 22.90  1
## Merc 280                       Merc 280 19.2   6 167.6 123 3.92 3.440 18.30  1
## Merc 280C                     Merc 280C 17.8   6 167.6 123 3.92 3.440 18.90  1
## Merc 450SE                   Merc 450SE 16.4   8 275.8 180 3.07 4.070 17.40  0
## Merc 450SL                   Merc 450SL 17.3   8 275.8 180 3.07 3.730 17.60  0
## Merc 450SLC                 Merc 450SLC 15.2   8 275.8 180 3.07 3.780 18.00  0
## Cadillac Fleetwood   Cadillac Fleetwood 10.4   8 472.0 205 2.93 5.250 17.98  0
## Lincoln Continental Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0
## Chrysler Imperial     Chrysler Imperial 14.7   8 440.0 230 3.23 5.345 17.42  0
## Fiat 128                       Fiat 128 32.4   4  78.7  66 4.08 2.200 19.47  1
## Honda Civic                 Honda Civic 30.4   4  75.7  52 4.93 1.615 18.52  1
## Toyota Corolla           Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1
## Toyota Corona             Toyota Corona 21.5   4 120.1  97 3.70 2.465 20.01  1
## Dodge Challenger       Dodge Challenger 15.5   8 318.0 150 2.76 3.520 16.87  0
## AMC Javelin                 AMC Javelin 15.2   8 304.0 150 3.15 3.435 17.30  0
## Camaro Z28                   Camaro Z28 13.3   8 350.0 245 3.73 3.840 15.41  0
## Pontiac Firebird       Pontiac Firebird 19.2   8 400.0 175 3.08 3.845 17.05  0
## Fiat X1-9                     Fiat X1-9 27.3   4  79.0  66 4.08 1.935 18.90  1
## Porsche 914-2             Porsche 914-2 26.0   4 120.3  91 4.43 2.140 16.70  0
## Lotus Europa               Lotus Europa 30.4   4  95.1 113 3.77 1.513 16.90  1
## Ford Pantera L           Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.50  0
## Ferrari Dino               Ferrari Dino 19.7   6 145.0 175 3.62 2.770 15.50  0
## Maserati Bora             Maserati Bora 15.0   8 301.0 335 3.54 3.570 14.60  0
## Volvo 142E                   Volvo 142E 21.4   4 121.0 109 4.11 2.780 18.60  1
##                     am gear carb
## Mazda RX4            1    4    4
## Mazda RX4 Wag        1    4    4
## Datsun 710           1    4    1
## Hornet 4 Drive       0    3    1
## Hornet Sportabout    0    3    2
## Valiant              0    3    1
## Duster 360           0    3    4
## Merc 240D            0    4    2
## Merc 230             0    4    2
## Merc 280             0    4    4
## Merc 280C            0    4    4
## Merc 450SE           0    3    3
## Merc 450SL           0    3    3
## Merc 450SLC          0    3    3
## Cadillac Fleetwood   0    3    4
## Lincoln Continental  0    3    4
## Chrysler Imperial    0    3    4
## Fiat 128             1    4    1
## Honda Civic          1    4    2
## Toyota Corolla       1    4    1
## Toyota Corona        0    3    1
## Dodge Challenger     0    3    2
## AMC Javelin          0    3    2
## Camaro Z28           0    3    4
## Pontiac Firebird     0    3    2
## Fiat X1-9            1    4    1
## Porsche 914-2        1    5    2
## Lotus Europa         1    5    2
## Ford Pantera L       1    5    4
## Ferrari Dino         1    5    6
## Maserati Bora        1    5    8
## Volvo 142E           1    4    2
test$names <- str_replace(test$names, " ", "-")
str_match(test$names,"-")
##       [,1]
##  [1,] "-" 
##  [2,] "-" 
##  [3,] "-" 
##  [4,] "-" 
##  [5,] "-" 
##  [6,] NA  
##  [7,] "-" 
##  [8,] "-" 
##  [9,] "-" 
## [10,] "-" 
## [11,] "-" 
## [12,] "-" 
## [13,] "-" 
## [14,] "-" 
## [15,] "-" 
## [16,] "-" 
## [17,] "-" 
## [18,] "-" 
## [19,] "-" 
## [20,] "-" 
## [21,] "-" 
## [22,] "-" 
## [23,] "-" 
## [24,] "-" 
## [25,] "-" 
## [26,] "-" 
## [27,] "-" 
## [28,] "-" 
## [29,] "-" 
## [30,] "-" 
## [31,] "-" 
## [32,] "-"
# I wanted to extract the names, then count how many of them are distinct with group by and summarise num=n() but I didn't have time.

Problem 7

The built-in matrix WorldPhones shows the usage of telephones by continent. Plot with a line chart the progression of usage across the years, where each continent gets its own segmented line and color.