Quiz: Analysis & Inference

Week 13 · Advanced Statistical Programming using R

Recap: Interactive Data Storytelling

Q1

What is the fastest way to make an existing ggplot2 figure explorable in the browser?

  1. Export it as PNG and add hover in Photoshop
  2. Rewrite it in JavaScript
  3. Wrap it in ggplotly()
  4. Convert it into a gt table

Answer: 3) ggplotly(p) turns a static ggplot2 object into an interactive plotly chart. Hover, zoom, and legend toggles come along for free.

Q2

What is an sf object in R?

  1. A special image file with map data
  2. A wrapper around Google Maps
  3. A raster grid of pixel values
  4. A data frame with a geometry column

Answer: 4) sf (Simple Features) objects behave like normal data frames. The geometry column stores the shape, and you can pipe through dplyr as usual.

R and Python at a Glance

Q3

Which language design contrast is correct?

  1. R is compiled, Python is interpreted
  2. R was designed for statistics; Python is general-purpose with data science added via libraries
  3. Python has data frames in the core language; R needs a library
  4. R has no NA, Python has None

Answer: 2) R was designed by statisticians for statistics. Python is general-purpose, and its data science tools live in libraries like pandas, numpy, and scikit-learn.

Q4

You write x[1] in Python and get the second element of the list. Why?

  1. Python has a bug in list indexing
  2. Lists are shuffled at creation time
  3. x[1] means “second-to-last” in Python
  4. Python indexes start at 0, so x[0] is the first element

Answer: 4) Python counts from 0. Slices like x[1:3] also exclude the end index. Both differ from R and are classic beginner traps.

Python Basics

Q5

Which Python code correctly defines a function that returns the sum of two numbers?

  1. function add(a, b) { return a + b }
  2. def add(a, b): return a + b
  3. add <- function(a, b) a + b
  4. add = (a, b) => a + b

Answer: 2) Python uses def, a colon, and an indented body. Unlike R, the last expression is not returned automatically, so return is mandatory.

Q6

Which R container is closest to a Python dictionary ({"anna": 31, "ben": 25})?

  1. data.frame
  2. matrix
  3. Named list, e.g. list(anna = 31, ben = 25)
  4. Atomic vector, e.g. c(31, 25)

Answer: 3) A named list stores key-value pairs like a dict. Tuples in Python have no direct R equivalent; sets map roughly to unique().

reticulate

Q7

What does the {reticulate} package do?

  1. Runs an embedded Python session inside R with automatic object conversion
  2. Wraps R into a Python package for Jupyter
  3. Compiles R code to Python bytecode
  4. Provides a REST API between an R and a Python server

Answer: 1) reticulate embeds Python in your R session. R and Python objects are translated on the fly, so a data frame becomes a pandas DataFrame and back.

Q8

In a Quarto document that uses {reticulate}, how do you read the R object penguins from a Python chunk?

  1. import(penguins)
  2. py$penguins
  3. read.csv("penguins.csv")
  4. r.penguins

Answer: 4) Inside Python, r.penguins fetches the R object. The reverse works with py$x inside R to reach a Python variable.

Q9

You call numpy::linspace(0, 1, num = 5) from R via reticulate and get an error. What is the most likely fix?

  1. Restart R
  2. Reinstall numpy
  3. Pass num = 5L so R sends an integer instead of a double
  4. Wrap the call in try()

Answer: 3) Many Python functions expect integers. R numbers are doubles by default, so use the L suffix (5L) to send an integer across the border.

Q10

Your project needs a random-forest classifier from scikit-learn but you want to keep wrangling and plotting in R. What is the recommended workflow?

  1. Rewrite the whole project in Python
  2. Wrangle in R, model in Python via reticulate, plot back in R
  3. Export CSV files between two separate scripts
  4. Drop the model and use a simpler R alternative

Answer: 2) Let each language do what it does best. reticulate moves data across the border without file exports, so R handles wrangling and plots while Python handles the model.