Viewing Data and Variables

In this lesson we will go over how the print function works and how we can use it to print text or a dataframe.

In programming, a simple yet fundamental function is one that displays the content of a variable. If we want to display the text "Hello World!" we pass this text as an argument to the print function.

Let's consider another situation. Imagine a loading screen during a game's installation process. This screen might show a percentage to indicate progress. It comprises static text and a dynamic number such as "Progress: 16%". The print function we just learned can combine these two things together with some difficulty. However, there is another function which makes the task easier, the cat function. "cat" stands for concatenate, which means to join together. The cat function joins together the arguments provided to it and prints them out. Let's see an example of this.

While the cat function is useful for combining text and numbers to print out, it has one drawback in comparison to print, and that is, it does not automatically start a new line with each call. This means that if we want to print out multiple lines of text, we need to embed within the text the \n character, which is a special character sequence, known as an escape sequence, that tells R to start a new line.

In the next code block, try running it first, then try running it again after deleting the escape sequence and see what happens!

Printing dataframes

The simplest way to print a dataframe is to include it inside a print statement. For instance, after creating the iris dataset, we can use print(iris). However, as this dataset has 150 rows, this will display the entire dataset, which may be a tad overwhelming.

Instead, we can use other functions called head or tail to view the first and last six rows, respectively. The number of displayed rows can be adjusted with an optional input.

There was something else we snuck in there and that is a comment. A comment begins a line with the pound symbol #, which tells R to ignore anything found after it. What might happen if we have an R program that contains only comments? Well, nothing will happen! So why are they important? Comments are added to code to help fellow data scientists, developers (or yourself a year from now) have a better understanding for why a line of code is written the way it is. This is incredibly useful in maintaining a code base as sometimes we need to understand the history / reason / purpose of a code chunk to really understand it.

We can further access a single column of data from the dataframe by writing the dataframe name and the column name separated by a dollar sign $. For instance, to access the Sepal.Length column, we write iris$Sepal.Length.

Practice exercise

Assign to a variable a the string Hello World. This will be printed out with the provided cat function.