Print Output
Part of the Fundamentals section of Coddy's R journey — lesson 26 of 78.
You've been using print() throughout this course to display output. Now let's take a closer look at how it actually works.
The print() function displays a value to the console. It works with any data type:
print("Hello, World!")
print(42)
print(TRUE)Each call to print() outputs on a new line. One thing to notice is that print() shows character strings with quotes around them:
print("Welcome") # Output: [1] "Welcome"The [1] you see is R's way of showing the index of the first element. This becomes useful when working with longer outputs, but for now, just know it's part of how R displays results.
You can print the result of an expression directly:
print(5 + 3) # Output: [1] 8
print(10 > 5) # Output: [1] TRUEIn the next lesson, you'll learn about cat(), which gives you more control over how your output appears.
Challenge
EasyUse the print() function to display different data types and the result of an expression.
Complete the following tasks in order:
- Print the character string
"R Programming" - Print the numeric value
3.14 - Print the logical value
FALSE - Print the result of the expression
25 - 7 - Print the result of the comparison
100 >= 50
Each value should be printed on its own line using separate print() calls.
Cheat sheet
The print() function displays values to the console and works with any data type:
print("Hello, World!")
print(42)
print(TRUE)Each print() call outputs on a new line. Character strings are displayed with quotes, and R shows [1] before output to indicate the index of the first element.
You can print the result of expressions directly:
print(5 + 3) # Output: [1] 8
print(10 > 5) # Output: [1] TRUETry it yourself
# TODO: Write your code below
# Use print() to display each value on its own line
# 1. Print the character string "R Programming"
# 2. Print the numeric value 3.14
# 3. Print the logical value FALSE
# 4. Print the result of the expression 25 - 7
# 5. Print the result of the comparison 100 >= 50This 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