// Numbas version: finer_feedback_settings {"name": "Querying a data frame (TV set)", "extensions": ["stats", "json_from_file"], "custom_part_types": [], "resources": [["question-resources/tv_u1CHb9S.json", "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:
install.packages(\"ncldata\", repos=\"http://R-Forge.R-project.org\")\n
library(ncldata)
data(tv)
Take a look at the help for the data frame with:
\nhelp(tv)\n", "advice": "
The dimensions of the data frame can be found with the function dim
dim(tv)\n
Or individually, the number of rows and columns can be found with
\nnrow(tv)\n
ncol(tv)
The row with a title {random_prog['Title']} can be isolated with
\ntv[tvTitle=='{random_prog['Title']}',]\nNote the capital T for the column title. If you aren't sure about the column names use
\ncolnames(tv)\n
We can either read off the rating, or we can isolate the value using the $ notation
\ntv[tvTitle=='{random_prog['Title']}',]$Rating\nFollowing the method in part b),
\ntv[tvTitle=='{random_prog2['Title']}',]$Votes\nUsing the square bracket notation above, the subset of tv shows with a start year greater than or equal to {year} can be found with
\ntv[tvStartYear >= {year},]\nOr, using the function subset,
subset(tv,StartYear >= {year})\nWe 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:
nrow(subset(tv,StartYear >= {year}))\nThe subset of TV shows which both started and ended in the {1900+(decade*10)}s can be found with
\nsubset(tv,StartYear >={1900+(decade*10)} & EndYear <={1909+(decade*10)})\nAgain, nrow can be used to find the number of shows in this subset.
The subset of TV shows which started and ended in the same year, but have a rating greater than {random_rating} is
\nsubset(tv,StartYear == EndYear & Rating > {random_rating})\nThe mean rating of TV shows which started have started since (and including) {year2} can be found with
\nmean(subset(tv,StartYear >= {year2})$Rating)\nThe subset of TV shows which are exactly {random_length} minutes long can be found with
\nsubset(tv, Length=={random_length})\nNote 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:
\ntv[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?
Rows: [[0]]
\nColumns: [[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"}], "type": "question", "contributors": [{"name": "Chris Graham", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/369/"}, {"name": "Chris Graham", "profile_url": "https://numbas-editor.mas.ncl.ac.uk/accounts/profile/73/"}], "resources": ["question-resources/tv_u1CHb9S.json"]}]}], "contributors": [{"name": "Chris Graham", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/369/"}, {"name": "Chris Graham", "profile_url": "https://numbas-editor.mas.ncl.ac.uk/accounts/profile/73/"}]}