Menu
Coddy logo textTech

Comments in R

Part of the Fundamentals section of Coddy's R journey — lesson 3 of 78.

Comments are notes you write inside your code. R completely ignores them - they exist only to help humans understand the code.

To write a comment, use the # symbol. Everything after # until the end of the line is ignored:

# This is a comment
print("Hello!")

A comment can also be written at the end of a line, after the code:

print("Hello!") # This prints Hello!

R does not have a special multi-line comment syntax. To write a comment over several lines, start each line with #:

# This is a multi-line comment.
# Each line starts with its own #
# and R ignores all of them.
print("Welcome!")

Comments can also temporarily disable a line of code without deleting it:

# print("This line will NOT run")
print("This line will run")
challenge icon

Challenge

Beginner

Fix the code so that only Hello, R! is printed.

  • Replace the ? with the symbol that turns a line into a comment
  • The line printing Goodbye! should become a comment so it does NOT run
  • Only change the line that starts with ?

Cheat sheet

Comments in R:

  • Notes for humans - R ignores them
  • Can disable code temporarily without deleting it

Single-line comment:

# This is a comment
print("Hello!") # Comment after code

Multi-line comments (use # on each line):

# This is a multi-line comment
# spanning several lines

Disabling code:

# print("This will NOT run")
print("This will run")

Try it yourself

# Type your code below
? print("Goodbye!")
print("Hello, R!")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals