DataCamp replacement
R foundations fallback
Use this local exercise only if classroom DataCamp access is unavailable. It covers the same required topics without a DataCamp account.
Create r-learning/r-foundations.R in your course repository. Type the code yourself and complete every TODO. Complete each part by the deadline shown. Your Git history records the earlier blocks even though you continue working in the same file.
Block 1 · Objects and basic operations
For Session 2 · Text Preprocessing Due Sep. 25 at 09:15
course <- "BA3 Text as Data"
sessions <- 6
uses_orange <- TRUE
# TODO: print each object.
# TODO: use class() to inspect each object's type.
# TODO: calculate 6 * 2 and store it as available_demo_points.
# TODO: compare available_demo_points with 12.
Block 2 · Vectors and matrices
For Session 3 · Descriptive Patterns Due Oct. 2 at 09:15
dates <- as.Date(c(
"2026-09-18", "2026-09-25", "2026-10-02",
"2026-10-09", "2026-10-16", "2026-10-30"
))
topics <- c(
"introduction", "preprocessing", "descriptive patterns",
"clustering", "sentiment", "topic modeling"
)
# TODO: print the third date and third topic.
# TODO: select the topics containing the letter "a" with grepl().
scores <- matrix(
c(8, 7, 9, 6, 8, 7),
nrow = 3,
byrow = TRUE,
dimnames = list(
c("student-a", "student-b", "student-c"),
c("setup", "preprocessing")
)
)
# TODO: print the matrix and select student-b's preprocessing score.
# TODO: calculate the mean for each assignment with colMeans().
Block 3 · Factors, data frames, and lists
For Session 4 · Clustering and Similarity Due Oct. 9 at 09:15
track <- factor(
c("standard", "review", "standard", "standard"),
levels = c("standard", "review")
)
# TODO: inspect the factor with levels() and table().
# TODO: select only the entries equal to "review".
schedule <- data.frame(
session = 1:6,
date = dates,
topic = topics
)
# TODO: inspect schedule with str(), head(), and summary().
# TODO: add a logical column named demo_required and set it to TRUE.
# TODO: save schedule as r-learning/schedule.csv with row.names = FALSE.
course_record <- list(
schedule = schedule,
track = "standard",
completed_blocks = 3
)
# TODO: inspect the list with str() and names().
# TODO: extract the topic column through the schedule item.
Block 4 · Conditionals and loops
For Session 5 · Classification and Sentiment Analysis Due Oct. 16 at 09:15
completed_tasks <- 5
required_tasks <- 6
# TODO: write an if / else statement that prints "complete"
# when the two values are equal and "keep working" otherwise.
# TODO: add an else-if branch that prints "almost complete"
# when exactly one task is missing.
block_scores <- c(1, 1, 0.5, 1, 1, 1)
# TODO: use a for loop to print each block number and score.
# TODO: use a second loop to count how many scores equal 1.
# TODO: print the number of complete blocks and the total score.
Block 5 · Functions, apply, and utilities
For Session 6 · Topic Modeling Due Oct. 30 at 09:15
# TODO: write a function named completion_rate() that accepts
# a vector of scores and returns their mean.
# TODO: call completion_rate(block_scores).
topic_terms <- list(
topic_1 = c("economy", "growth", "trade"),
topic_2 = c("security", "peace", "dialogue")
)
# TODO: use lapply() to return the number of terms in each topic.
# TODO: use sapply() to return the first term in each topic.
filenames <- c(
"demo 1 README.md",
"demo_02_output.png",
"topic-results.csv"
)
# TODO: replace spaces with hyphens using gsub().
# TODO: print today's date with Sys.Date().
# TODO: inspect the completed objects with str().
Submission
Commit and push the following files.
r-learning/r-foundations.Rwith every TODO completed.r-learning/schedule.csvcreated by the script.r-learning/r-foundations-output.txtcontaining the printed output from all five blocks.
To capture the output after finishing the script, run these commands in the RStudio Console from the repository root.
sink("r-learning/r-foundations-output.txt")
source("r-learning/r-foundations.R", echo = TRUE)
sink()
These three files replace the five DataCamp classroom completion records. Each block follows the same deadline as the DataCamp version. You may troubleshoot with classmates, but the files in your repository must be your own.