Strings & Dates

Strings and dates are how R handles text and time — cleaning, joining, and searching text, and parsing real dates so you can do arithmetic and extract parts like the year or weekday.

Learn Strings & Dates 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.

By the end of this lesson you'll manipulate text with paste(), substr(), and case functions, find and replace patterns with grepl() and gsub(), and parse and format real dates with as.Date() and format().

What You'll Learn in This Lesson

1️⃣ Working with Strings

Base R covers the essentials: nchar() for length, toupper() / tolower() for case, trimws() to strip spaces, paste() / paste0() to join, and substr() to slice. All are vectorized.

2️⃣ Finding and Replacing Patterns

grepl() tests which strings contain a pattern; sub() replaces the first match and gsub() replaces all; strsplit() breaks a string apart. These accept plain text or regular expressions.

3️⃣ Real Dates

as.Date() turns text into a proper Date you can do arithmetic on (in days). format() extracts and reformats parts, and weekdays() names the day.

Your turn. Fill in the # TODO blank, run it, and compare with the expected output.

Write it from the outline, run it, and check it against the example output. Subtracting two Dates gives a time difference — wrap it in as.numeric() for a plain number of days.

📋 Quick Reference — Strings & Dates

Practice quiz

Which function joins strings with NO separator?

  • paste()
  • paste0()
  • cat()
  • join()

Answer: paste0(). paste0() concatenates with no separator; paste() uses a space by default.

What does nchar('Hello') return?

  • 5
  • 1
  • "Hello"
  • 0

Answer: 5. nchar() counts the number of characters, including spaces.

Which function strips surrounding whitespace from a string?

  • toupper()
  • trimws()
  • substr()
  • gsub()

Answer: trimws(). trimws() removes leading and trailing whitespace.

What does substr('programming', 1, 4) return?

  • "ming"
  • "gram"
  • "prog"
  • "prgm"

Answer: "prog". substr() slices characters from position 1 to 4 inclusive.

Which function parses 'YYYY-MM-DD' text into a Date?

  • as.Date()
  • strptime.only()
  • date()
  • to.Date()

Answer: as.Date(). as.Date("2024-03-15") returns a proper Date object.

What is the difference between sub() and gsub()?

  • They are identical
  • sub() replaces the first match; gsub() replaces all
  • gsub() replaces only the first match
  • sub() works only on numbers

Answer: sub() replaces the first match; gsub() replaces all. sub() changes the first match; gsub() ('global') changes every match.

What does d + 30 produce when d is a Date?

  • An error
  • 30 appended as text
  • A Date 30 days later
  • The number 30

Answer: A Date 30 days later. Date arithmetic works in days, so d + 30 is 30 days later.

Which format code gives the four-digit year?

  • "%Y"
  • "%m"
  • "%d"
  • "%B"

Answer: "%Y". format(d, "%Y") returns the four-digit year; %m is month number, %B month name.

What does weekdays(d) return for a Date?

  • The month number
  • The day of the month
  • The name of the weekday
  • The year

Answer: The name of the weekday. weekdays() returns the day-of-week name, e.g. "Friday".

Why convert dates to the Date class instead of leaving them as text?

  • It saves disk space
  • Text looks nicer
  • It changes the time zone
  • Dates support arithmetic, comparison, and chronological sorting

Answer: Dates support arithmetic, comparison, and chronological sorting. Real Dates can be subtracted, compared, and sorted chronologically; text cannot.