Checkpoint: R Basics

A checkpoint is a consolidation lesson where you prove you can combine the skills from the previous module — here, the core of base R — by solving a realistic, multi-step problem.

Learn Checkpoint: R Basics in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll recap data types, subsetting, missing data, vectorization, regex, frequency tables, error handling, and environments, then tackle a build challenge that chains them together and test yourself with a short quiz.

What This Checkpoint Covers

1️⃣ The 60-Second Recap

Every idea from R Basics, in one runnable block. If any line surprises you, that's your cue to revisit the matching lesson.

2️⃣ Build Challenge: Clean a Survey Column

This single task chains regex cleaning , type coercion , NA handling , and a frequency table — the exact pipeline you'll run on real data. Fill in the three blanks, run it, and predict the output before you check the solution.

Here's a complete, commented solution. The key moves: gsub("[^0-9]", "", raw) keeps only digits, coercing the leftover blanks to NA , then na.rm = TRUE and table() summarise the clean data.

Run it in RStudio or rdrr.io/snippets to confirm every line.

3️⃣ Checkpoint Quiz

Answer each in your head first, then expand to check. If you miss one, the topic in brackets tells you which lesson to revisit.

"character" . A vector holds one type, so R coerces everything to the most flexible present — and character can represent any value, so it wins over numeric and logical.

lst[["a"]] or lst$a . Single brackets lst["a"] would keep the list wrapper; [[ ]] and $ extract the element itself.

NA propagates — any arithmetic touching it is unknown. Add na.rm = TRUE : sum(c(1, NA, 3), na.rm = TRUE) gives 4 .

x + 1 . The single value 1 is recycled across every element, so no loop is needed.

"12" . The negated class [^0-9] matches every non-digit, and gsub deletes them all, leaving only the digits.

The first creates/updates a local count that disappears when the function returns. The second ( ) reaches outward to update an existing count in an enclosing environment.

Practice quiz

What does class(c(1, "2", TRUE)) return?

  • "numeric"
  • "logical"
  • "character"
  • "list"

Answer: "character". A vector holds a single type, so R coerces to the most flexible present — character can represent any value, so it wins.

Given lst <- list(a = 1:3), which expression returns the bare vector 1 2 3?

Double brackets [[ ]] (or $) extract the element itself; single brackets ["a"] keep the list wrapper.

What is the result of sum(c(1, NA, 3))?

  • 4
  • 0
  • An error
  • NA

Answer: NA. NA propagates through arithmetic. Add na.rm = TRUE to ignore it and get 4.

How do you make mean(c(1, NA, 3)) return 2 instead of NA?

  • Use mean(c(1, NA, 3), skip = TRUE)
  • Use mean(c(1, NA, 3), na.rm = TRUE)
  • Wrap it in suppressWarnings()
  • Use na.omit() on the result

Answer: Use mean(c(1, NA, 3), na.rm = TRUE). na.rm = TRUE drops NA values before computing, so the mean of 1 and 3 is 2.

What is the idiomatic, vectorized way to add 1 to every element of x?

  • x + 1
  • for (i in x) i + 1
  • sapply(x, +1)
  • add(x, 1)

Answer: x + 1. The scalar 1 is recycled across every element, so no loop is needed — that is vectorization.

What does gsub("[^0-9]", "", "a1b2") produce?

  • "ab"
  • "a1b2"
  • "12"
  • "1 2"

Answer: "12". The negated class [^0-9] matches every non-digit, and gsub deletes them, leaving only the digits.

What does grepl("^a", c("apple", "berry")) return?

  • c(FALSE, TRUE)
  • c(TRUE, FALSE)
  • c(TRUE, TRUE)
  • "apple"

Answer: c(TRUE, FALSE). ^a anchors the match to the start of the string, so only "apple" matches.

What does table(c("x", "y", "x")) compute?

  • The sum of the values
  • The unique values only
  • An error (non-numeric)
  • A frequency count of each category

Answer: A frequency count of each category. table() builds a contingency table — here counting x twice and y once.

Inside a function, what does count <<- count + 1 do differently from count <- count + 1?

  • Nothing — they are identical
  • <<- reaches outward to update count in an enclosing environment
  • <<- creates a faster local copy
  • <<- deletes count after assignment

Answer: <<- reaches outward to update count in an enclosing environment. The super-assignment <<- modifies an existing variable in an enclosing/global environment instead of creating a local one.

What does tryCatch(log(-1), warning = function(w) NA) return?

  • NaN
  • An error
  • NA
  • -Inf

Answer: NA. log(-1) raises a warning, which the handler catches and replaces with NA.