r comments
r comments

R Comments

Posted on

R comments

Comments are for explaining R code, and for making it more readable. Comments are also for preventing execution when testing alternative code. Comments starts with a #. R will ignore anything that starts with # when executing code.

Single line comments

Example using a comment before a line of code:

# This is a comment
"Hello!"

> # This is a comment
> “Hello!”
[1] “Hello!”

example using a comment at the end of a line of code:

"Hello!"  # This is a comment

> “Hello!” # This is a comment
[1] “Hello!”

Multiline Comments

There are no syntax in R for multiline comments. However, we can just insert a # for each line to create multiline comments:

# This is a comment
# in multiline
# more than just one line
"Hello!"

> # This is a comment
> # in multiline
> # more than just one line
> “Hello!”
[1] “Hello!”