1. What is a data frame?
It is time to learn about one of the most common and useful data structures in R. The data frame.
2. Data frame
A data frame is used to store a table of data and is nothing more than a grid with rows and columns, and you have likely used them already in other programs. You might say, wait, isn't this just a matrix? Not quite! While a data frame has a similar look of a matrix, in that it has rows and columns, it differs in that you can have multiple data types in a single data frame. This means that you could put a character vector in one column, and a numeric vector in another, like what is done here.
3. Data frames and friends
To see this in action, let's go back to the example of Dan and Rob owing you money. Instead of binding together the vectors like we did with matrices, we could create a data frame to hold this information. That would look something like this. Notice how there is a character column, name, to represent who owes you the money, and a numeric column, payment, to represent how much they owe you. Each column represents a unique variable, and each row represents an observation of data. This type of structure of variables in columns and observations in rows is extremely powerful, and will make your life infinitely easier when trying to subset, plot, or summarize your data later on. Creating the data frame is very similar to the other data structures that you have created so far. You just pass in each vector that you want added to the data frame. Just remember that each vector should be the same length!
4. Name that frame!
If you want to change the names of your columns, you can do that using the colnames function. For example, if you want to rename the "name" column as "friend", and the "payment" column as "money", you can see that the process is very similar to naming vectors. The character vector of "friend" and "money" is assigned to the colnames of debt. Also, like vectors, this could have been specified during the creation of the data frame.
5. Let's practice!
In the next few exercises, you will become a business owner and learn to create a data frame of your new company's cash flows. Along the way, you will learn some functions for inspecting different parts of your new shiny data frame. Have fun!