← Back to R Course | Chapter 1: Getting Started | Lesson 4 of 6

Comments

A comment starts with a hash mark and is a note R ignores, like a sticky note for the reader. It explains what your code does without changing it.

In this page:

  1. Comments
Syntax
r
# This is a comment
x <- value  # trailing comment

Comments

A # starts a comment in R, and everything after it on that line is ignored by the interpreter. R has no multi-line comment syntax, so every comment line needs its own #.

Comments should clarify why code does something non-obvious rather than restate what's already clear from the code.

Note: RStudio and most editors let you comment/uncomment a whole selected block at once, adding # to every line.

Example: Comments

markup
# This script demonstrates comments
x <- 10  # inline comment after code
# print(x) -- this line is commented out and won't run
print(x)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to use // or /* */ for comments, which are errors in R; only # starts a comment.
  2. Trying to comment a block with a single # and forgetting each line needs its own #.
  3. Putting # inside a string like "a#b" and thinking it starts a comment, when inside quotes it is ordinary text.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.