# What is the paste() function in R?

The method `paste()` in R enables you to **combine strings, numerical values and other data types**. It converts all the elements into strings.

## What is paste in R used for?

R’s `paste()` function is used to **combine different elements and create strings**. The number of arguments it takes can vary, and those arguments can be strings or other data types. It then returns a string that connects the elements with each other. The function allows for concatenations in both horizontal and vertical form.

The ability to control the formatting of the resulting string makes `paste()` particularly useful for **text processing in R**. And if you combine it with functions like `sprintf()` and `paste0()`, you can perform complex text manipulation and present data in a very readable form. This is useful for axis labels for graphics, the formatting for output reports and compilations of variable names.

## What is the basic syntax of paste() in R?

The `paste()` function in R offers various ways to format strings, including options for working with **empty fields** and inserting user-defined placeholders.

The basic syntax consists of the following:

```R
paste(x, sep=" ", collapse=NULL)
```

- **`x`**: The parameter `x` stands for the elements that you want to combine
- **`sep`**: This is where you can define a separator, which will appear between the combined elements. The default is a space.
- **`collapse`**: This parameter is useful for combing the elements from a vector into a single string

## Examples of how to use paste() in R

Below we’ll show examples of `paste()` in R and some of its applications in text processing and data visualisation.

### Simple concatenations

Let’s start with the simplest use of `paste()` in R – combining elements, without using the `sep` or `collapse` parameters. The **default values** for these parameters will be applied.

```R
result <- paste("Hello", "World")
# result = "Hello World"
```

Here we see that the two elements `"Hello"` and `"World"` have been combined into a single string, with a space between them. The default value of `sep` is a space and that of `collapse` is `NULL`. You can enter as many arguments into R’s `paste()` function as you want and combine them with each other.

### How to use the separator parameter

You can use the parameter `sep` to customise which character serves as a separator.

```R
result <- paste("Apple", "Banana", "Orange", sep = ", ")
# result = "Apple, Banana, Orange"
```

In this example, we combine the strings `"Apple"`, `"Banana"` and `"Orange"`, with a comma and space between them as a separator. We didn’t define `collapse` in this example, so the result is a string where the combined elements are only divided by a separator.

### How to use collapse

The separator parameter `sep` doesn’t work as expected when `paste()` is applied to a vector. This is where `collapse` comes in. You can use `collapse` to define the symbol or value that will separate the elements of a vector when they are combined into a single string.

```R
paste(c(0,40,33,15,7,98), collapse = "-")
# result = "0-40-33-15-7-98"
```

In the above example, we set the `collapse` parameter to `-`. That means the elements from the vector will be separated by a dash in the resulting string.

### Using paste() with both sep and collapse

If you’re working with vectors, you can define both `sep` and `collapse` as parameters.

```R
paste(c('a', 'b'), 1:10, sep = '-', collapse = ' and ')
# result = "a-1 and b-2 and a-3 and b-4 and a-5 and b-6 and a-7 and b-8 and a-9 and b-10"
```

The result of the above operation is a string in which the elements from the first vector (`'a' 'b'`) are combined with the elements of the second vector (numbers 1-10).

Tip In our guide, you can also read about other R functions for text manipulation, such as [R substring()](https://www.ionos.com/en-ie/digitalguide/websites/web-development/r-substring/) and R gsub() and sub().


This is a markdown version of: [https://www.ionos.com/en-ie/digitalguide/websites/web-development/r-paste/](https://www.ionos.com/en-ie/digitalguide/websites/web-development/r-paste/) for AI/LLM consumption.