print(): Print a value of object to the console

We use <- to assign values to the variables.

We can also assign values using = instead of <-, but we recommend against using = to avoid confusion.

a <- 1
b <- -3

To see the value stored in a variable, we simply ask R to evaluate a and it shows the stored value:

a
#> [1] 1

A more explicit way to ask R to show us the value stored in a is using print like this:

print(a)
#> [1] 1

Instruction

# Assign three variables a <- 1 b <- -3 # print variable 'a' without 'print' function # display variable 'b' by using 'print' function # Assign three variables a <- 1 b <- -3 # print variable 'a' without 'print' function a # display variable 'b' by using 'print' function print(b) test_object("a", undefined_msg = "Make sure to define a variable `a`.", incorrect_msg = "Make sure that you assign the correct value to `a`.") test_object("b", undefined_msg = "Make sure to define a variable `b`.", incorrect_msg = "Make sure that you assign the correct value to `b`.") test_output_contains("a", incorrect_msg = "Make sure that you should type a to print out value in variable 'a'.") test_output_contains("print(b)", incorrect_msg = "Make sure that you should use print function to print out value in variable 'b'.") test_function("print") success_msg("Great! Head over to the next exercise.")

Previous: 1-1 | Basic functions in R

Next: 1-3 | data(): Prebuilt datasets

Back to Main