# Split Code Over Multiple Lines in R (3 Examples)

In this post, I’ll illustrate how to write long code over several lines in the R programming language.

The post will contain this content:

Let’s start right away.

## Example 1: Writing Equation Over Multiple Lines

This Example illustrates how to divide the code of an equation over multiple lines in an R script.

First, let’s execute an equation in only one line:

```x1a <- 1 + 2 + 3 # Equation in one line x1a # Print output to console # 6```

The result of our equation is 6.

If we want to write our equation over multiple lines (e.g. because it is very long), we can split the R syntax as shown below:

```x1b <- 1 + 2 + # Equation in multiple lines 3 x1b # Print output to console # 6```

The result is exactly the same as in the previous R code where we executed the equation in only one line of code.

It is important to note that we put the + sign at the end of the first line. By doing this, we tell R that more code is following.

## Example 2: Writing Character String Over Multiple Lines

The following syntax shows how to create a character string in multiple lines of an R script. Again, let’s first run our code in only one line.

```x2a <- "this is a string" # String in one line x2a # Print output to console #  "this is a string"```

The output of the previous R code is shown in the RStudio console: “this is a string”.

Now, let’s create this character string over multiple lines:

```x2b <- "this is a string" # String in multiple lines x2b # Print output to console #  "this \nis \na \nstring"```

This time, the output is the character string “this \nis \na \nstring”.

Note that this output contains “\n” at each point where we used a new line of code. In other words: the on-line output is slightly different compared to the multi-line output.

However, the next example shows how to create exactly the same character string using multiple lines of code. So keep on reading!

## Example 3: Writing Character String Over Multiple Lines Using paste() Function

This Example illustrates how to create a character string in several lines of code using the paste0 function.

Let’s first create a character string in one line:

```x3a <- "this is another string" # String in one line x3a # Print output to console #  "this is another string"```

The output is the following character string: “this is another string”

We can use the paste0 function (or the paste function) to create exactly the same output in multiple lines of code:

```x3b <- paste0("this ", # String in multiple lines "is ", "another ", "string") x3b # Print output to console #  "this is another string"```

Looks good!

## Video, Further Resources & Summary

Do you need further information on the R programming codes of this tutorial? Then you may have a look at the following video of my YouTube channel. In the video, I explain the content of this post:

Furthermore, you could read some of the related articles on my website:

In this article you learned how to split codes over multiple script lines in R programming. In case you have additional questions, let me know in the comments section.

Subscribe to my free statistics newsletter