Comments can be used to explain the code, and to make it more readable. The comments can also be used to prevent execution when testing alternative code.
Comment in R
Comments in R are generic English sentences used for code readability, prevent code execution, including resources, or explanation of the code or Metadata of the project. Comments have nothing to do with the logic of the code.
Types of Comments in Programming
There are three types of comments in a programming language.
- Single-line comments: This kind of comment needs only one line.
- Multi-line comments: This kind of comment requires more than one line.
- Documentation comments: This kind of comment is conscripted for a quick documentation lookup.
Comment Support in R
R language only supports single-line comments and does not support multi-line or documentation comments.
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")
}
If you run the above code, then comment does not have any impact on this example.
If you only write the comment in the code, then it will not produce any output.
# This is the first comment in this example
If you run the above code, then it won’t produce any output.
Multiline Comments
As we have mentioned that multiline comments are not 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.

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.