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
BeginnerFix 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 codeMulti-line comments (use # on each line):
# This is a multi-line comment
# spanning several linesDisabling code:
# print("This will NOT run")
print("This will run")Try it yourself
# Type your code below
? print("Goodbye!")
print("Hello, R!")This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values