LAISHA
Open Laisha

Royal Statistical Society professional awards Programming for Statistics & Data (R and Python): practice questions

Royal Statistical Society professional awards (Data Analyst, GradStat, CStat) · United Kingdom · Royal Statistical Society

Original practice questions written to the published syllabus · not past papers
10worked here, free
21written to this exam in this subject
7syllabus units

What this subject covers

Writing correct, readable programs for data work: R and Python as the working languages, with the control structures, functions, data frames, vectorisation and debugging habits that later computing subjects assume. C and SAS are covered where agricultural universities still teach them.

In the exams: GATE DA 'Programming, Data Structures and Algorithms' (heavy: Python named); AICE-JRF Computer Application Unit 2 programming (heavy); AIEEA-PG Group 07 computer programming (present)

Where the 21 questions fall

How our questions for Royal Statistical Society professional awards spread across the units of Programming for Statistics & Data (R and Python). Difficulty: 3 easy, 16 medium, 2 hard.

R: vectors, data frames, factors and lists4
Reading, cleaning and reshaping data4
Graphics in R (base and ggplot2) and Python (matplotlib)4
Debugging, testing and code style3
R: functions, control flow and vectorisation2
Python: core types, NumPy arrays and pandas data frames2
Writing functions and simple packages2

10 practice questions, worked

Choose an answer, then open the working. Each explanation says why the right answer is right and why the others are not.

1 · R: vectors, data frames, factors and lists · medium

In R, what does c(1, 2, 3, 4) + c(10, 20) return?

  1. 11 22 13 24
  2. 11 22 3 4
  3. 11 22
  4. An error
Answer and working

A. 11 22 13 24

The shorter vector is recycled: 10, 20, 10, 20.

Further reading: Wickham and Grolemund, R for Data Science; McKinney, Python for Data Analysis (2022); Kernighan and Ritchie, The C Programming Language (1988); Delwiche and Slaughter, The Little SAS Book (2019)

2 · R: vectors, data frames, factors and lists · hard

In R, 0.1 + 0.2 == 0.3 evaluates to FALSE. The correct way to compare is:

  1. isTRUE(all.equal(0.1 + 0.2, 0.3))
  2. as.integer(0.1 + 0.2) == 0.3
  3. round(0.1 + 0.2) == 0.3
  4. 0.1 + 0.2 = 0.3
Answer and working

A. isTRUE(all.equal(0.1 + 0.2, 0.3))

Floating-point values are inexact; all.equal compares within a tolerance.

Further reading: Wickham and Grolemund, R for Data Science; McKinney, Python for Data Analysis (2022); Kernighan and Ritchie, The C Programming Language (1988); Delwiche and Slaughter, The Little SAS Book (2019)

3 · R: functions, control flow and vectorisation · medium

Which R function computes the mean yield for each variety, given vectors yield and variety?

  1. lapply(yield, mean)
  2. apply(yield, 1, mean)
  3. mean(yield, variety)
  4. tapply(yield, variety, mean)
Answer and working

D. tapply(yield, variety, mean)

tapply applies a function within groups defined by a factor.

Further reading: Wickham and Grolemund, R for Data Science; McKinney, Python for Data Analysis (2022); Kernighan and Ritchie, The C Programming Language (1988); Delwiche and Slaughter, The Little SAS Book (2019)

4 · Reading, cleaning and reshaping data · medium

A left join of a yield table (100 plots) to a treatment table by plot identifier gives 112 rows. The most likely cause is:

  1. Missing yields in some of the plots in the yield table
  2. Duplicated plot keys in the treatment table
  3. Wrong column types
  4. The join was an inner join
Answer and working

B. Duplicated plot keys in the treatment table

Duplicate keys multiply matching rows.

Further reading: Wickham and Grolemund, R for Data Science; McKinney, Python for Data Analysis (2022); Kernighan and Ritchie, The C Programming Language (1988); Delwiche and Slaughter, The Little SAS Book (2019)

5 · Reading, cleaning and reshaping data · medium

Before analysing a newly read data set, which check is least informative?

  1. Counting missing values per column
  2. Checking ranges and types
  3. Printing the first value of each column only
  4. Listing distinct values of categorical variables
Answer and working

C. Printing the first value of each column only

One value reveals little; missing counts, ranges and categories find most problems.

Further reading: Wickham and Grolemund, R for Data Science; McKinney, Python for Data Analysis (2022); Kernighan and Ritchie, The C Programming Language (1988); Delwiche and Slaughter, The Little SAS Book (2019)

6 · Graphics in R (base and ggplot2) and Python (matplotlib) · medium

In ggplot2, writing colour = "red" inside aes() instead of outside it:

  1. Colours the points red and suppresses every legend
  2. Gives an error
  3. It makes a legend entry 'red', not red points
  4. Has no effect
Answer and working

C. It makes a legend entry 'red', not red points

Inside aes() the string is treated as a data value; outside it sets a fixed colour.

Further reading: Wickham and Grolemund, R for Data Science; McKinney, Python for Data Analysis (2022); Kernighan and Ritchie, The C Programming Language (1988); Delwiche and Slaughter, The Little SAS Book (2019)

7 · Graphics in R (base and ggplot2) and Python (matplotlib) · hard

Why do ggplot2 plots usually need data in long (tidy) form?

  1. ggplot2 cannot read numbers in wide form
  2. Long form uses less memory
  3. Wide form cannot be saved
  4. Each aesthetic maps to one column
Answer and working

D. Each aesthetic maps to one column

Mapping colour to year needs a year column, not one column per year.

Further reading: Wickham and Grolemund, R for Data Science; McKinney, Python for Data Analysis (2022); Kernighan and Ritchie, The C Programming Language (1988); Delwiche and Slaughter, The Little SAS Book (2019)

8 · Debugging, testing and code style · medium

A unit test for a variance function might check that the values 1, 2, 3, 4, 5 give:

  1. 2
  2. 2.5
  3. 1.58
  4. 3
Answer and working

B. 2.5

Σ(x − 3)² = 10; 10/(5 − 1) = 2.5; 2 divides by n.

Further reading: Wickham and Grolemund, R for Data Science; McKinney, Python for Data Analysis (2022); Kernighan and Ritchie, The C Programming Language (1988); Delwiche and Slaughter, The Little SAS Book (2019)

9 · Debugging, testing and code style · medium

A minimal reproducible example helps debugging mainly because it:

  1. Makes the program run faster
  2. It isolates the cause in minimal code
  3. Removes the need for tests
  4. Hides confidential data
Answer and working

B. It isolates the cause in minimal code

Stripping away the irrelevant often reveals the bug.

Further reading: Wickham and Grolemund, R for Data Science; McKinney, Python for Data Analysis (2022); Kernighan and Ritchie, The C Programming Language (1988); Delwiche and Slaughter, The Little SAS Book (2019)

10 · Debugging, testing and code style · medium

Replacing the number 12.5 in several formulas with a named constant plot_area_m2 mainly:

  1. Speeds up arithmetic
  2. Is required by the compiler
  3. Clearer, easier-to-change code
  4. Removes rounding errors
Answer and working

C. Clearer, easier-to-change code

Magic numbers hide meaning and invite inconsistent edits.

Further reading: Wickham and Grolemund, R for Data Science; McKinney, Python for Data Analysis (2022); Kernighan and Ritchie, The C Programming Language (1988); Delwiche and Slaughter, The Little SAS Book (2019)

Ten here, 21 in the app. The Laisha app has every practice question we have written to Royal Statistical Society professional awards in this subject, marked as you go, with timed papers, a jump-to-unanswered list and a certificate when you pass at 70 per cent.

Free for ten days, then one subscription of US$10 a month. We would rather say so here than have you find it later.

Practise the full set in Laisha →

Read the subject

Programming for Statistics & Data (R and Python)The full study manual, free Royal Statistical Society professional awardsFormat, sections and how it has changed

Programming for Statistics & Data (R and Python) for other exams

ICAR-IASRI MSc and PhD · India

Ask Laisha about this

Opens Laisha's chat with your question ready to send. Sign in or start free.