Sketch – Data Trivia
[This article was first published on Rstats – OUseful.Info, the blog…, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
A bit more tinkering with F1 data from the ergast db, this time trying to generating trivia / facts around races.
The facts are identified using SQL queries:
#starts for a team q=paste0('SELECT d.code, COUNT(code) AS startsforteam, c.name AS name FROM drivers d JOIN races r JOIN results rs JOIN constructors c WHERE c.constructorId=rs.constructorId AND d.driverId=rs.driverId AND r.raceId=rs.raceId AND d.code IN (',driversThisYear_str,') ',upto,' GROUP BY d.code, c.name HAVING (startsforteam+1) % 50 = 0') startsTeammod50=dbGetQuery(ergastdb, q) #looking for poles to date modulo 5 q=paste0('SELECT d.code, COUNT(code) AS poles FROM drivers d JOIN qualifying q JOIN races r WHERE r.raceId=q.raceId AND d.code IN (',driversThisYear_str,') AND d.driverId=q.driverId AND q.position=1',upto,' GROUP BY code HAVING poles>1 AND (poles+1) % 5 = 0') lookingpolesmod5=dbGetQuery(ergastdb, q)
Some of the queries also embed query fragments, which I intend to develop further…
upto=paste0(' AND (year<',year,' OR (year=',year,' AND round<',round,')) ')
I'm using knitr to generate Github flavoured markdown (gfm) from my Rmd docs – here’s part of the header:
--- output: md_document: variant: gfm ---
The following recipe then takes results from the trivia queries and spiels the output:
if (nrow(startsTeammod50)>0) { for (row in 1:nrow(startsTeammod50)) { text = '- `r startsTeammod50[row, "code"]` is looking for their `r toOrdinal(startsTeammod50[row, "startsforteam"]+1)` start for `r startsTeammod50[row, "name"]`' cat(paste0(knit_child(text=text,quiet=TRUE),'\n')) } } if (nrow(lookingpolesmod5)>0) { for (row in 1:nrow(lookingpolesmod5)) { text = '- `r lookingpolesmod5[row, "code"]` is looking for their `r toOrdinal(lookingpolesmod5[row, "poles"]+1)` ever pole position' cat(paste0(knit_child(text=text,quiet=TRUE),'\n')) } }
We then get outputs of the form:
- BOT is looking for their 100th race start
- HAM is looking for their 100th start for Mercedes
See more example outputs here: Bahrain F1 2018 – Race Trivia.
This is another recipe I need to work up a little further and add to Wrangling F1 Data With R.
To leave a comment for the author, please follow the link and comment on their blog: Rstats – OUseful.Info, the blog….
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.