If you want to take our Introduction to R course, here is the link.
What's a data frame?
You may remember from the chapter about matrices that all the elements that you put in a matrix should be of the same type. Back then, your data set on Star Wars only contained numeric elements.
When doing a market research survey, however, you often have questions such as:
- 'Are you married?' or 'yes/no' questions (
- 'How old are you?' (
- 'What is your opinion on this product?' or other 'open-ended' questions (
The output, namely the respondents' answers to the questions formulated above, is a data set of different data types. You will often find yourself working with data sets that contain different data types instead of only one.
A data frame has the variables of a data set as columns and the observations as rows. This will be a familiar concept for those coming from different statistical software packages such as SAS or SPSS.
Click 'Submit Answer'. The data from the built-in example data frame
mtcars will be printed to the console.
Quick, have a look at your data set
Wow, that is a lot of cars!
Working with large data sets is not uncommon in data analysis. When you work with (extremely) large data sets and data frames, your first task as a data analyst is to develop a clear understanding of its structure and main elements. Therefore, it is often useful to show only a small part of the entire data set.
Have a look at the structure
The total number of observations (e.g. 32 car types) The total number of variables (e.g. 11 car features) A full list of the variables names (e.g.
cyl ... ) The data type of each variable (e.g.
num) The first observations Applying the
str() function will often be the first thing that you do when receiving a new data set or data frame. It is a great way to get more insight in your data set before diving into the real analysis.
Investigate the structure of
mtcars. Make sure that you see the same numbers, variables, and data types as mentioned above.
Creating a data frame
Since using built-in data sets is not even half the fun of creating your own data sets, the rest of this chapter is based on your personally developed data set. Put your jet pack on because it is time for some space exploration!
As a first goal, you want to construct a data frame that describes the main characteristics of eight planets in our solar system. According to your good friend Buzz, the main features of a planet are:
- The type of planet (Terrestrial or Gas Giant).
- The planet's diameter relative to the diameter of the Earth.
- The planet's rotation across the sun relative to that of the Earth.
- If the planet has rings or not (TRUE or FALSE).
After doing some high-quality research on Wikipedia, you feel confident enough to create the necessary vectors:
rings; these vectors have already been coded up on the right. The first element in each of these vectors correspond to the first observation.
You construct a data frame with the
data.frame() function. As arguments, you pass the vectors from before: they will become the different columns of your data frame. Because every column has the same length, the vectors you pass should also have the same length. But don't forget that it is possible (and likely) that they contain different types of data.
Use the function
data.frame() to construct a data frame. Pass the vectors
rings as arguments to
data.frame(), in this order. Call the resulting data frame
If you want to learn more from this course, here is the link
Related Courses in R
← Back to tutorial