// Numbas version: exam_results_page_options {"name": "Querying a data frame (TV set)", "extensions": ["stats", "json_from_file"], "custom_part_types": [], "resources": [["question-resources/tv_u1CHb9S.json", "/srv/numbas/media/question-resources/tv_u1CHb9S.json"]], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false, "typeendtoleave": false}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"name": "Querying a data frame (TV set)", "tags": [], "metadata": {"description": "

This question takes in a json file containing a tv data frame in the R package ncldata (https://rdrr.io/rforge/ncldata/). 

", "licence": "Creative Commons Attribution 4.0 International"}, "statement": "

Run the following commands to load the tv data frame:

\n
install.packages(\"ncldata\", repos=\"http://R-Forge.R-project.org\")
library(ncldata)
data(tv)
\n

Take a look at the help for the data frame with:

\n
help(tv)
\n

", "advice": "

a)

\n

The dimensions of the data frame can be found with the function dim

\n
dim(tv)
\n

Or individually, the number of rows and columns can be found with

\n
nrow(tv)
ncol(tv)
\n

b)

\n

The row with a title {random_prog['Title']} can be isolated with

\n
tv[tvTitle=='{random_prog['Title']}',]
\n

Note the capital T for the column title. If you aren't sure about the column names use

\n
colnames(tv)
\n

We can either read off the rating, or we can isolate the value using the $ notation

\n
tv[tvTitle=='{random_prog['Title']}',]$Rating
\n

c)

\n

Following the method in part b),

\n
tv[tvTitle=='{random_prog2['Title']}',]$Votes
\n

d)

\n

Using the square bracket notation above, the subset of tv shows with a start year greater than or equal to {year} can be found with

\n
tv[tvStartYear >= {year},]
\n

Or, using the function subset,

\n
subset(tv,StartYear >= {year})
\n

We can use nrow to find the number of tv shows in the subset, either by assigning the above to a new data frame, or querying it directly:

\n
nrow(subset(tv,StartYear >= {year}))
\n

e)

\n

The subset of TV shows which both started and ended in the {1900+(decade*10)}s can be found with

\n
subset(tv,StartYear >={1900+(decade*10)} & EndYear <={1909+(decade*10)})
\n

Again, nrow can be used to find the number of shows in this subset.

\n

f)

\n

The subset of TV shows which started and ended in the same year, but have a rating greater than {random_rating} is

\n
subset(tv,StartYear == EndYear & Rating > {random_rating})
\n

g)

\n

The mean rating of TV shows which started have started since (and including) {year2} can be found with

\n
mean(subset(tv,StartYear >= {year2})$Rating)
\n

h)

\n

The subset of TV shows which are exactly {random_length} minutes long can be found with

\n
subset(tv, Length=={random_length})
\n

Note that for this example, you will find a different (and incorrect) answer if you use square brackets to subset. Take a look at the output of the following command to see the problem:

\n
tv[tvLength == {random_length},]$Length
", "rulesets": {}, "extensions": ["json_from_file", "stats"], "variables": {"year2": {"name": "year2", "group": "Ungrouped variables", "definition": "random(2011..2017)", "description": "", "templateType": "anything"}, "random_prog_id": {"name": "random_prog_id", "group": "Ungrouped variables", "definition": "random(20..length(getTVEnded()))", "description": "", "templateType": "anything"}, "tv_in_decade": {"name": "tv_in_decade", "group": "Ungrouped variables", "definition": "len(filter(prog[\"StartYear\"] >= 1900+(decade*10) and prog[\"EndYear\"] <= 1909+(decade*10),prog,getTVEnded()))", "description": "", "templateType": "anything"}, "columns_in_data": {"name": "columns_in_data", "group": "Ungrouped variables", "definition": "len(getTVData()[1])-1", "description": "", "templateType": "anything"}, "random_prog": {"name": "random_prog", "group": "Ungrouped variables", "definition": "getTVEnded()[random_prog_id-1]", "description": "", "templateType": "anything"}, "one_year_shows": {"name": "one_year_shows", "group": "Ungrouped variables", "definition": "len(filter(prog[\"Rating\"] > random_rating and (prog[\"StartYear\"] = prog[\"EndYear\"]),prog,getTVEnded()))", "description": "", "templateType": "anything"}, "random_prog_id2": {"name": "random_prog_id2", "group": "Ungrouped variables", "definition": "random(20..length(getTVEnded()) except random_prog_id)", "description": "", "templateType": "anything"}, "rows_in_data": {"name": "rows_in_data", "group": "Ungrouped variables", "definition": "len(getTVData())", "description": "", "templateType": "anything"}, "rating_since_year": {"name": "rating_since_year", "group": "Ungrouped variables", "definition": "mean(map(prog[\"Rating\"],prog,filter(prog[\"StartYear\"]>=year2,prog,getTVData())))", "description": "", "templateType": "anything"}, "random_rating": {"name": "random_rating", "group": "Ungrouped variables", "definition": "random(5..9)", "description": "", "templateType": "anything"}, "random_length": {"name": "random_length", "group": "Ungrouped variables", "definition": "random(20..90#10)", "description": "", "templateType": "anything"}, "tv_exact_length": {"name": "tv_exact_length", "group": "Ungrouped variables", "definition": "length(filter(prog[\"Length\"]=random_length,prog,getTVData()))", "description": "", "templateType": "anything"}, "decade": {"name": "decade", "group": "Ungrouped variables", "definition": "random(6..9)", "description": "", "templateType": "anything"}, "category2": {"name": "category2", "group": "Movie Categories", "definition": "categories[1]", "description": "", "templateType": "anything"}, "random_prog2": {"name": "random_prog2", "group": "Ungrouped variables", "definition": "getTVEnded()[random_prog_id2-1]", "description": "", "templateType": "anything"}, "tv_after_year": {"name": "tv_after_year", "group": "Ungrouped variables", "definition": "length(filter(prog[\"StartYear\"]>=year,prog,getTVData()))", "description": "", "templateType": "anything"}, "category1": {"name": "category1", "group": "Movie Categories", "definition": "categories[0]", "description": "", "templateType": "anything"}, "year": {"name": "year", "group": "Ungrouped variables", "definition": "random(1990..2017)", "description": "", "templateType": "anything"}, "categories": {"name": "categories", "group": "Movie Categories", "definition": "shuffle([\"Action\", \"Animation\", \"Comedy\", \"Drama\", \"Documentary\", \"Romance\"])", "description": "

The 7 movie categories in the data frame

", "templateType": "anything"}}, "variablesTest": {"condition": "", "maxRuns": 100}, "ungrouped_variables": ["year", "year2", "random_prog_id", "random_prog_id2", "random_length", "decade", "random_rating", "tv_after_year", "rows_in_data", "columns_in_data", "tv_in_decade", "random_prog", "one_year_shows", "rating_since_year", "tv_exact_length", "random_prog2"], "variable_groups": [{"name": "Movie Categories", "variables": ["categories", "category1", "category2"]}], "functions": {"getTVData": {"parameters": [], "type": "list", "language": "jme", "definition": "json_from_file('resources/question-resources/tv_u1CHb9S.json')"}, "getTVEnded": {"parameters": [], "type": "list", "language": "jme", "definition": "filter(tv['EndYear']<>'NA',tv,getTVData())"}}, "preamble": {"js": "question.signals.on('HTMLAttached',function() {\n $(\"span.dollar\").html(\"$\");\n});", "css": ""}, "parts": [{"type": "gapfill", "useCustomName": false, "customName": "", "marks": 0, "showCorrectAnswer": true, "showFeedbackIcon": true, "scripts": {}, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "adaptiveMarkingPenalty": 0, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "prompt": "

How many rows and columns are there in the tv data frame?

\n

Rows: [[0]]

\n

Columns: [[1]]

", "gaps": [{"type": "numberentry", "useCustomName": false, "customName": "", "marks": "0.5", "showCorrectAnswer": true, "showFeedbackIcon": true, "scripts": {}, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "adaptiveMarkingPenalty": 0, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "minValue": "rows_in_data", "maxValue": "rows_in_data", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}, {"type": "numberentry", "useCustomName": false, "customName": "", "marks": "0.5", "showCorrectAnswer": true, "showFeedbackIcon": true, "scripts": {}, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "adaptiveMarkingPenalty": 0, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "minValue": "columns_in_data", "maxValue": "columns_in_data", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}], "sortAnswers": false}, {"type": "numberentry", "useCustomName": false, "customName": "", "marks": 1, "showCorrectAnswer": true, "showFeedbackIcon": true, "scripts": {}, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "adaptiveMarkingPenalty": 0, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "prompt": "

What is the average rating of the TV show {random_prog['Title']}?

", "minValue": "random_prog['Rating']", "maxValue": "random_prog['Rating']", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "precisionType": "dp", "precision": "1", "precisionPartialCredit": 0, "precisionMessage": "You have not given your answer to the correct precision.", "strictPrecision": false, "showPrecisionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}, {"type": "numberentry", "useCustomName": false, "customName": "", "marks": 1, "showCorrectAnswer": true, "showFeedbackIcon": true, "scripts": {}, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "adaptiveMarkingPenalty": 0, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "prompt": "

How many people have voted on the rating of the TV show {random_prog2['Title']}?

", "minValue": "random_prog2['Votes']", "maxValue": "random_prog2['Votes']", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}, {"type": "numberentry", "useCustomName": false, "customName": "", "marks": 1, "showCorrectAnswer": true, "showFeedbackIcon": true, "scripts": {}, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "adaptiveMarkingPenalty": 0, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "prompt": "

How many of the TV programmes have a start year of {year} or later?

\n

", "minValue": "tv_after_year", "maxValue": "tv_after_year", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}, {"type": "numberentry", "useCustomName": false, "customName": "", "marks": 1, "showCorrectAnswer": true, "showFeedbackIcon": true, "scripts": {}, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "adaptiveMarkingPenalty": 0, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "prompt": "

How many of the TV shows started and ended in the same year, but have a rating greater than {random_rating}?

", "minValue": "one_year_shows", "maxValue": "one_year_shows", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}, {"type": "numberentry", "useCustomName": false, "customName": "", "marks": 1, "showCorrectAnswer": true, "showFeedbackIcon": true, "scripts": {}, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "adaptiveMarkingPenalty": 0, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "prompt": "

How many of the TV shows both started and ended in the {1900+(decade*10)}s?

", "minValue": "tv_in_decade", "maxValue": "tv_in_decade", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}, {"type": "numberentry", "useCustomName": false, "customName": "", "marks": 1, "showCorrectAnswer": true, "showFeedbackIcon": true, "scripts": {}, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "adaptiveMarkingPenalty": 0, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "prompt": "

What is the mean rating of TV shows which have started since (and including) {year2}?

", "minValue": "rating_since_year", "maxValue": "rating_since_year", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "precisionType": "dp", "precision": "1", "precisionPartialCredit": 0, "precisionMessage": "You have not given your answer to the correct precision.", "strictPrecision": false, "showPrecisionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}, {"type": "numberentry", "useCustomName": false, "customName": "", "marks": 1, "showCorrectAnswer": true, "showFeedbackIcon": true, "scripts": {}, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "adaptiveMarkingPenalty": 0, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "prompt": "

How many TV shows are exactly {random_length} minutes long?

", "minValue": "tv_exact_length", "maxValue": "tv_exact_length", "correctAnswerFraction": false, "allowFractions": false, "mustBeReduced": false, "mustBeReducedPC": 0, "showFractionHint": true, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain"}], "contributors": [{"name": "Chris Graham", "profile_url": "https://numbas-editor.mas.ncl.ac.uk/accounts/profile/73/"}, {"name": "Chris Graham", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/369/"}]}]}], "contributors": [{"name": "Chris Graham", "profile_url": "https://numbas-editor.mas.ncl.ac.uk/accounts/profile/73/"}, {"name": "Chris Graham", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/369/"}]}