Open In App

Split Code Over Multiple Lines in R

Last Updated : 31 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to split code over multiple lines in R programming language.

Method 1: Writing Equation Over Multiple Lines

In this approach for splitting the code over multiple lines with writing equation over multiple lines, the user just needs to work with the syntax part only as in this approach there is no need for any function, here the user will be just using the mathematical operator(*,/,+,-) and this will be acting as the joining operator, which will be used at the end of the line and the user can continue to the next line and this can be performed multiple time. The user can go up to any number of code split in the R programming language.

Example: In this example, we will be writing Equation Over Multiple Lines to split the code over multiple lines in the R programming language.

R




gfg_1=1 +
5 +
7 +
9
  
gfg_1


Output:

[1] 22

Example: In this example, we will be writing Equation Over Multiple Lines to split the code over multiple lines in the R programming language.

R




gfg_2=1 + 
5 -
7 *
9
  
gfg_2


Output:

[1] -57

Method 2: Writing Character String Over Multiple Lines

This approach is the easier approach among all, where the user just needs to use the “” operator to make the code split over the multiple lines, at the starting of the  “” operator will be there before the code starts and the “” operator will be at the end of the code splitter over multiple lines, and this will split the code over multiple lines by writing Character String Over Multiple Line in R programming language.

Example: In this example, we will be splitting the code over multiple lines by writing Character String Over Multiple Lines in the R programming language.

R




gfg="A 
Computer 
Science 
portal 
for 
geeks" 
  
gfg


Output:

[1] “A \nComputer \nScience \nportal \nfor \ngeeks”

Method 3: Using paste0) Function

In this approach to split the code over multiple lines with writing character string over multiple lines using the paste() function, the user just needs to call the paste function, which is an in-built function of the R language, and then needs to code which will split over multiples lines as the parameters of this function and further this will lead to the splitting of the code over multiple lines in R programming language.

paste0() function is used for concatenating vectors after converting to characters.

Syntax:

paste0(…, collapse = NULL)

Parameters:

  • … :  one or more R objects, to be converted to character vectors.
  • collapse: an optional character string to separate the results. Not NA_character_.

Example: R program to split code over multiple lines

R




gfg <- paste0("A ",            
              "Computer Science ",
              "portal for ",
              "geeks")
gfg                              


Output:

[1] A Computer Science portal for geeks



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads