R Comments (Single and Multiline)

In R, you can add comments to your code using the “hash symbol (#)”. Any text following the hash symbol on the same line will be treated as a comment and will not be executed. Comments help explain your code, making it more readable and understandable.

Single-Line Comments in R

R only supports single-line comments drafted by a “#” symbol. Single-line comments are comments that require only one line. 

Syntax

# comment statement

Example

# This is the first comment in this example

data_1 <- 101

if (data_1 > 99) {
 print("You have hit the century")
} else {
 print("You are out in nervous ninty")
}

The comment does not impact this example if you run the above code.

If you only write the comment in the code, it will not produce any output.

# This is the first comment in this example

If you run the above code, it won’t produce any output.

Multiline Comments

Multiline comments are not directly supported in R language, but we can just insert a “#” for each line to create multiline comments.

# This is the first comment in this example
# The second comment
# written in
# more than just one line

data_1 <- 101

if (data_1 > 99) {
 print("You have hit the century")
} else {
 print("You are out in nervous ninty")
}

That’s it for the comments in R.

Leave a Comment