R (4.6) Module 7
Importing and exporting data interview questions
Interview questions on importing and exporting data: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
A column of fees comes back from read.csv() as text. What usually causes it, and how do you find the values responsible?
Answered in the lesson Reading and writing CSV and text data
-
Fresher
What does haven's as_factor() do to an SPSS column with value labels, and when should you not call it yet?
-
Fresher
What does jsonlite's fromJSON() return for an array of JSON objects, and how does that differ from what httr2's resp_body_json() returns?
Answered in the lesson JSON, web APIs and responsible scraping
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Fresher to mid
A nightly R script reads a supplier's CSV with read_csv() and sums the fee column. It has run for months; one morning the total is 40 % lower, and nothing failed. How do you find what changed, and how do you make the script stop next time instead of reporting a wrong total?
- What it tests
- Type guessing and its limits, missing-value codes, the traps of parse_number(), col_types with problems() and stop_for_problems(), and checks on counts and totals.
- First step
- Read yesterday's file and today's with the same call and compare the column specification readr printed, the number of rows, the number of NA fees and a few raw lines. A new code for missing fees, a currency word or a few values that are not plain numbers change what readr returns without stopping the script.
- Follow-ups
-
- The fee column is still numeric, but some fees are a thousand times too small. What could do that?
- How would you test the import before it runs on real data?
- Where should the check live, in the script, in the scheduler or in the database?
From the lesson Reading and writing CSV and text data
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
Two analysts read the same SPSS survey of 400 people into R. One reports a mean age of 38.9, the other 37.5, and a third gets NA with a warning. The file codes Refused as -9, marked as missing. Explain how all three results can come from one file, and which is right.
- What it tests
- User-defined missing values, what read_sav(user_na = TRUE) keeps, why mean() ignores is.na() on labelled codes, and how foreign::read.spss() turns labelled columns into factors.
- First step
- Ask each analyst for their read call. read_sav() with the defaults turns -9 into NA; with user_na = TRUE the -9 values stay numbers that mean() adds in; foreign::read.spss() makes a labelled column a factor, and the mean of a factor is NA with a warning.
- Follow-ups
-
- How many people refused, if the two means are 38.9 and 37.5?
- How would you make the read the same for everyone on the team?
- Should refusals be reported at all, and how?
From the lesson Excel, SPSS, Stata and SAS files
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A field team sends a workbook every week: a title in row 1, the header in row 3, notes under the table, a codes sheet, and dates that are sometimes date cells and sometimes plain numbers. Your import breaks every few weeks. How do you make it robust, and how do you know when it has gone wrong?
- What it tests
- Reading by sheet name and anchor rather than a fixed range, naming column types, converting Excel date numbers, decoding from the codes sheet, and checks that fail loudly on new layouts.
- First step
- Collect the workbooks that broke the import and list what changed each time: rows added, a sheet renamed, a new code, a date typed as text. Then write the import as steps that each check their own assumption, starting from the sheet names and the header row.
- Follow-ups
-
- One week the dates are text such as 14/06/26. What do you do?
- The team adds a fourth variety without updating the codes sheet. What happens, and how do you catch it?
- Would you ask the team to change the workbook, and what would you ask for?
From the lesson Excel, SPSS, Stata and SAS files
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Your script pages through an API 100 records at a time with req_perform_iterative() and iterate_with_offset(). The first page says total 2,345, but the table has 2,000 rows and no error appeared. What happened, and how do you make the download complete and checked?
- What it tests
- The defaults of req_perform_iterative(), knowing the last page, checking counts against the total, offset pagination over changing data, rate limits and testing downloads without the network.
- First step
- 2,000 is exactly 20 pages of 100, and 20 is the default of max_reqs, so the loop stopped by itself. Pass max_reqs = Inf with resp_pages or resp_complete so the loop knows the last page, then compare nrow() with the total.
- Follow-ups
-
- Records are added while you download, and some ids appear twice. Why, and what do you change?
- The API answers 429 halfway through. How do you keep what you have and finish later?
- How would you test the download without calling the API?
From the lesson JSON, web APIs and responsible scraping
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A teammate's analysis calls a paid weather API with the key written into the script, and prints each request while debugging. The repository is about to become public. What are the risks, and what exactly do you change before it goes public?
- What it tests
- Treating an exposed key as burned, environment variables and .Renviron, what printed requests and logs reveal, redaction in httr2, and a check that keeps keys out of scripts.
- First step
- Assume the key is already exposed: it is in the history, and in any output the prints produced. Revoke it with the provider and get a new one before anything else, then change the code so the new key is read from an environment variable and never printed.
- Follow-ups
-
- The analysis also runs every night on a server. Where does the key live there?
- A collaborator needs to run it too. How do they get a key?
- How do you make sure no key comes back into the repository?
From the lesson JSON, web APIs and responsible scraping
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes