0Pricing
R Academy · Lesson

Building Strings with paste() and paste0()

Concatenate strings with separators using paste and paste0.

Building Strings with paste() and paste0() is a free R Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the R Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

paste() Basics

paste() converts its arguments to character strings and concatenates them with a separator. By default the separator is a single space ' '. It is the most versatile string-building function in base R.

paste('Hello', 'World')
paste('User', 'ID', ':', 42)
paste('R', 'version', '4.3.1')

paste0() — Zero-Separator Shortcut

paste0() is identical to paste(..., sep=''). It concatenates strings with no separator between them. This is the go-to function when building file paths, column names, or URLs programmatically.

paste0('file_', 1:5, '.csv')
paste0('col_', c('a', 'b', 'c'))
paste0('/home/user/', 'data.csv')

Changing the Separator with sep

The sep argument controls what is placed between each argument. Use it to build CSV rows, file paths, date strings, or any delimited output.

# Hyphen-separated date
paste('2024', '05', '29', sep = '-')
# Slash-separated path
paste('home', 'user', 'docs', sep = '/')
# Comma-separated
paste('Alice', 'Bob', 'Carol', sep = ', ')

Vectorized paste()

paste() is vectorized — when given vectors, it pairs up elements position by position (recycling shorter vectors if needed). The result has the same length as the longest input vector.

labels <- c('Item', 'Item', 'Item')
numbers <- c(1, 2, 3)
paste(labels, numbers)
# Shorter form using recycling
paste('Item', 1:5)

Collapsing a Vector into One String

The collapse argument joins all elements of the result vector into a single string, using the collapse value as the separator between elements. Without collapse, you get a character vector; with it, you get a single string.

fruits <- c('apple', 'banana', 'cherry')
# Without collapse: character vector of length 3
paste('I like', fruits)
# With collapse: single string
paste('I like', fruits, collapse = ' and ')

sep and collapse Together

You can use both sep and collapse in the same call. First sep joins the argument vectors element-wise, then collapse joins the resulting character vector into one string.

first <- c('John', 'Jane', 'Bob')
last  <- c('Doe', 'Smith', 'Brown')
# sep joins first+last, collapse joins all three names
paste(first, last, sep = ' ', collapse = ', ')

paste0() for Building Variable Names

paste0() is commonly used to programmatically build variable or column names, file paths, and SQL-like strings without any separator noise.

# Build column names
col_names <- paste0('Q', 1:5, '_score')
print(col_names)
# Build file paths
years <- 2020:2023
files <- paste0('data/report_', years, '.csv')
print(files)

Numeric Coercion in paste()

paste() automatically converts numbers, logicals, and other types to character strings. You do not need to call as.character() first — paste() handles it transparently.

n <- 42
pi_val <- 3.14159
flag <- TRUE
paste('Count:', n)
paste('Pi =', pi_val)
paste('Active:', flag)
paste0('n=', n, ' pi=', round(pi_val, 2))

Creating CSV-Like Rows

Combine vectorized paste() with collapse = ',' to create simple CSV output strings from a data frame without loading any external package.

names_vec  <- c('Alice', 'Bob', 'Carol')
scores_vec <- c(85, 92, 78)
rows <- paste(names_vec, scores_vec, sep = ',')
cat(paste(rows, collapse = '\n'))

NA Handling in paste()

Unlike many R functions, paste() converts NA to the literal string 'NA' rather than propagating the missing value. This is useful but can surprise users who expect silent handling of missing values.

x <- c('Hello', NA, 'World')
paste(x, '!')
# NA becomes the string 'NA'
result <- paste0('Value: ', NA)
print(result)
print(class(result))

Practical Patterns: paste() in Reports

Paste shines when generating human-readable report strings. Combine paste0(), round(), and paste() with collapse to build summary sentences from data.

mean_score <- 83.7
n_students  <- 28
high_score  <- 99
msg <- paste0(
  'Class summary: ', n_students, ' students, ',
  'mean score ', round(mean_score, 1), ', ',
  'top score ', high_score, '.'
)
cat(msg)

Quick Check

What does the collapse argument in paste() do?

paste() and paste0(): Key Takeaways

Key takeaways for paste() and paste0():

  • paste(a, b) joins with a space; paste0(a, b) joins with no separator
  • sep controls the string placed between each argument
  • collapse reduces the resulting vector to a single string
  • Both functions are vectorized and recycle shorter arguments
  • NA is converted to the literal string 'NA'
  • Numbers and logicals are automatically coerced to character
# sep vs collapse at a glance
x <- c('a', 'b', 'c')
y <- c(1, 2, 3)
cat('paste (sep=-)  :', paste(x, y, sep = '-'), '\n')
cat('paste (collapse):', paste(x, collapse = '+'), '\n')
cat('paste0          :', paste0(x, y), '\n')

Frequently asked questions

Is the “Building Strings with paste() and paste0()” lesson free?

Yes — the full text of “Building Strings with paste() and paste0()” is free to read here on the web, and the R Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the R Academy course, upgrade to CoddyKit PRO.

What will I learn in “Building Strings with paste() and paste0()”?

Concatenate strings with separators using paste and paste0. You practise R Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start R Academy?

No prior experience is required. R Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Building Strings with paste() and paste0()” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this R Academy lesson?

Yes. Every R Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Building Strings with paste() and paste0()
  2. Formatted Output with sprintf()
  3. Displaying Output with cat() and print()
  4. String Padding and Alignment
← Back to R Academy