数据框 Data frames

Most data sets you will be working with will be stored as data frames. By the end of this chapter focused on R basics, you will be able to create a data frame, select interesting parts of a data frame and order a data frame according to certain variables.

What’s a data frame?

Quick, have a look at your data set

Have a look at the structure

Creating a data frame

Creating a data frame (2)

Selection of data frame elements

Selection of data frame elements (2)

Only planets with rings

Only planets with rings (2)

Only planets with rings but shorter

Sorting

Sorting your data frame

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 (logical) ‘How old are you?’ (numeric) ‘What is your opinion on this product?’ or other ‘open-ended’ questions (character) … 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.

INSTRUCTIONS 70 XP Click ‘Submit Answer’. The data from the built-in example data frame mtcars will be printed to the console.

Show Answer (-70 XP) HINT Just click ‘Submit Answer’ and witness the magic!
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFByaW50IG91dCBidWlsdC1pbiBSIGRhdGEgZnJhbWVcbm10Y2FycyAifQ==

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.

So how to do this in R? Well, the function head() enables you to show the first observations of a data frame. Similarly, the function tail() prints out the last observations in your data set.

Both head() and tail() print a top line called the ‘header’, which contains the names of the different variables in your data set.

INSTRUCTIONS 70 XP Call head() on the mtcars data set to have a look at the header and the first observations.

Show Answer (-70 XP) HINT head(mtcars) will show the first observations of the mtcars data frame.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIENhbGwgaGVhZCgpIG9uIG10Y2Fyc1xuaGVhZChtdGNhcnMpIn0=

So, what do we have in this data set? For example, hp represents the car’s horsepower; the Datsun has the lowest horse power of the 6 cars that are displayed. For a full overview of the variables’ meaning, type ?mtcars in the console and read the help page. Continue to the next exercise!

Have a look at the structure Another method that is often used to get a rapid overview of your data is the function str(). The function str() shows you the structure of your data set. For a data frame it tells you:

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. mpg, 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.

INSTRUCTIONS 70 XP Investigate the structure of mtcars. Make sure that you see the same numbers, variables and data types as mentioned above.

Show Answer (-70 XP) HINT Use the str() function on mtcars.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIEludmVzdGlnYXRlIHRoZSBzdHJ1Y3R1cmUgb2YgbXRjYXJzXG5zdHIobXRjYXJzKSJ9

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: name, type, diameter, rotation and 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.

INSTRUCTIONS 70 XP INSTRUCTIONS 70 XP Use the function data.frame() to construct a data frame. Pass the vectors name, type, diameter, rotation and rings as arguments to data.frame(), in this order. Call the resulting data frame planets_df.

Show Answer (-70 XP) HINT Your data.frame() call starts as follows:

data.frame(planets, type, diameter)

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERlZmluaXRpb24gb2YgdmVjdG9yc1xubmFtZSA8LSBjKFwiTWVyY3VyeVwiLCBcIlZlbnVzXCIsIFwiRWFydGhcIiwgXCJNYXJzXCIsIFwiSnVwaXRlclwiLCBcIlNhdHVyblwiLCBcIlVyYW51c1wiLCBcIk5lcHR1bmVcIilcbnR5cGUgPC0gYyhcIlRlcnJlc3RyaWFsIHBsYW5ldFwiLCBcIlRlcnJlc3RyaWFsIHBsYW5ldFwiLCBcIlRlcnJlc3RyaWFsIHBsYW5ldFwiLCBcbiAgICAgICAgICBcIlRlcnJlc3RyaWFsIHBsYW5ldFwiLCBcIkdhcyBnaWFudFwiLCBcIkdhcyBnaWFudFwiLCBcIkdhcyBnaWFudFwiLCBcIkdhcyBnaWFudFwiKVxuZGlhbWV0ZXIgPC0gYygwLjM4MiwgMC45NDksIDEsIDAuNTMyLCAxMS4yMDksIDkuNDQ5LCA0LjAwNywgMy44ODMpXG5yb3RhdGlvbiA8LSBjKDU4LjY0LCAtMjQzLjAyLCAxLCAxLjAzLCAwLjQxLCAwLjQzLCAtMC43MiwgMC42NylcbnJpbmdzIDwtIGMoRkFMU0UsIEZBTFNFLCBGQUxTRSwgRkFMU0UsIFRSVUUsIFRSVUUsIFRSVUUsIFRSVUUpXG5cbiMgQ3JlYXRlIGEgZGF0YSBmcmFtZSBmcm9tIHRoZSB2ZWN0b3JzXG5wbGFuZXRzX2RmIDwtIGRhdGEuZnJhbWUobmFtZSwgdHlwZSwgZGlhbWV0ZXIsIHJvdGF0aW9uLCByaW5ncykifQ==

Creating a data frame (2) The planets_df data frame should have 8 observations and 5 variables. It has been made available in the workspace, so you can directly use it.

INSTRUCTIONS 70 XP Use str() to investigate the structure of the new planets_df variable.

Show Answer (-70 XP) HINT planets_df is already available in your workspace, so str(planets_df) will do the trick.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIENoZWNrIHRoZSBzdHJ1Y3R1cmUgb2YgcGxhbmV0c19kZlxuc3RyKHBsYW5ldHNfZGYpIn0=

Now that you have a clear understanding of the planets_df data set, it’s time to see how you can select elements from it. Learn all about in the next exercises!

Selection of data frame elements Similar to vectors and matrices, you select elements from a data frame with the help of square brackets [ ]. By using a comma, you can indicate what to select from the rows and the columns respectively. For example:

my_df[1,2] selects the value at the first row and second column in my_df. my_df[1:3,2:4] selects rows 1, 2, 3 and columns 2, 3, 4 in my_df. Sometimes you want to select all elements of a row or column. For example, my_df[1, ] selects all elements of the first row. Let us now apply this technique on planets_df!

INSTRUCTIONS 70 XP From planets_df, select the diameter of Mercury: this is the value at the first row and the third column. Simply print out the result. From planets_df, select all data on Mars (the fourth row). Simply print out the result. Show Answer (-70 XP) HINT To select the diameter for Venus (the second row), you would need: planets_df[2,3]. What do you need for Mercury then?

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBwbGFuZXRzX2RmIGRhdGEgZnJhbWUgZnJvbSB0aGUgcHJldmlvdXMgZXhlcmNpc2UgaXMgcHJlLWxvYWRlZFxuXG4jIFByaW50IG91dCBkaWFtZXRlciBvZiBNZXJjdXJ5IChyb3cgMSwgY29sdW1uIDMpXG5wbGFuZXRzX2RmWzEsM11cblxuIyBQcmludCBvdXQgZGF0YSBmb3IgTWFycyAoZW50aXJlIGZvdXJ0aCByb3cpXG5wbGFuZXRzX2RmWzQsIF0ifQ==

Apart from selecting elements from your data frame by index, you can also use the column names. To learn how, head over to the next exercise.

Selection of data frame elements (2) Instead of using numerics to select elements of a data frame, you can also use the variable names to select columns of a data frame.

Suppose you want to select the first three elements of the type column. One way to do this is

planets_df[1:3,2] A possible disadvantage of this approach is that you have to know (or look up) the column number of type, which gets hard if you have a lot of variables. It is often easier to just make use of the variable name:

planets_df[1:3,“type”] INSTRUCTIONS 70 XP Select and print out the first 5 values in the “diameter” column of planets_df.

Show Answer (-70 XP) HINT You can select the first five values with planets_df[1:5, …]. Can you fill in the … bit to only select the “diameter” column?
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBwbGFuZXRzX2RmIGRhdGEgZnJhbWUgZnJvbSB0aGUgcHJldmlvdXMgZXhlcmNpc2UgaXMgcHJlLWxvYWRlZFxuXG4jIFNlbGVjdCBmaXJzdCA1IHZhbHVlcyBvZiBkaWFtZXRlciBjb2x1bW5cbnBsYW5ldHNfZGZbMTo1LCBcImRpYW1ldGVyXCJdIn0=

Only planets with rings You will often want to select an entire column, namely one specific variable from a data frame. If you want to select all elements of the variable diameter, for example, both of these will do the trick:

planets_df[,3] planets_df[,“diameter”] However, there is a short-cut. If your columns have names, you can use the $ sign:

planets_df$diameter INSTRUCTIONS 70 XP Use the $ sign to select the rings variable from planets_df. Store the vector that results as rings_vector. Print out rings_vector to see if you got it right. Show Answer (-70 XP) HINT planets_df$diameter selects the diameter column from planets_df; what do you need to select the rings column then?

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIHBsYW5ldHNfZGYgaXMgcHJlLWxvYWRlZCBpbiB5b3VyIHdvcmtzcGFjZVxuXG4jIFNlbGVjdCB0aGUgcmluZ3MgdmFyaWFibGUgZnJvbSBwbGFuZXRzX2RmXG5yaW5nc192ZWN0b3IgPC0gcGxhbmV0c19kZiRyaW5nc1xuXG4jIFByaW50IG91dCByaW5nc192ZWN0b3JcbnJpbmdzX3ZlY3RvciJ9

Only planets with rings (2) You probably remember from high school that some planets in our solar system have rings and others do not. Unfortunately you can not recall their names. Could R help you out?

If you type rings_vector in the console, you get:

[1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE This means that the first four observations (or planets) do not have a ring (FALSE), but the other four do (TRUE). However, you do not get a nice overview of the names of these planets, their diameter, etc. Let’s try to use rings_vector to select the data for the four planets with rings.

INSTRUCTIONS 70 XP The code on the right selects the name column of all planets that have rings. Adapt the code so that instead of only the name column, all columns for planets that have rings are selected.

Show Answer (-70 XP) HINT Remember that to select all columns, you simply have to leave the columns part inside the [ ] empty! This means you’ll need [rings_vector, ].

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIHBsYW5ldHNfZGYgYW5kIHJpbmdzX3ZlY3RvciBhcmUgcHJlLWxvYWRlZCBpbiB5b3VyIHdvcmtzcGFjZVxuXG4jIEFkYXB0IHRoZSBjb2RlIHRvIHNlbGVjdCBhbGwgY29sdW1ucyBmb3IgcGxhbmV0cyB3aXRoIHJpbmdzXG5wbGFuZXRzX2RmW3JpbmdzX3ZlY3RvciwgXSJ9

Only planets with rings but shorter So what exactly did you learn in the previous exercises? You selected a subset from a data frame (planets_df) based on whether or not a certain condition was true (rings or no rings), and you managed to pull out all relevant data. Pretty awesome! By now, NASA is probably already flirting with your CV ;-).

Now, let us move up one level and use the function subset(). You should see the subset() function as a short-cut to do exactly the same as what you did in the previous exercises.

subset(my_df, subset = some_condition) The first argument of subset() specifies the data set for which you want a subset. By adding the second argument, you give R the necessary information and conditions to select the correct subset.

The code below will give the exact same result as you got in the previous exercise, but this time, you didn’t need the rings_vector!

subset(planets_df, subset = rings) INSTRUCTIONS 70 XP Use subset() on planets_df to select planets that have a diameter smaller than Earth. Because the diameter variable is a relative measure of the planet’s diameter w.r.t that of planet Earth, your condition is diameter < 1.

Show Answer (-70 XP) HINT subset(planets_df, subset = …) almost solves it; can you fill in the …?

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIHBsYW5ldHNfZGYgaXMgcHJlLWxvYWRlZCBpbiB5b3VyIHdvcmtzcGFjZVxuXG4jIFNlbGVjdCBwbGFuZXRzIHdpdGggZGlhbWV0ZXIgPCAxXG5zdWJzZXQocGxhbmV0c19kZiwgc3Vic2V0ID0gZGlhbWV0ZXIgPCAxKSJ9

Sorting Making and creating rankings is one of mankind’s favorite affairs. These rankings can be useful (best universities in the world), entertaining (most influential movie stars) or pointless (best 007 look-a-like).

In data analysis you can sort your data according to a certain variable in the data set. In R, this is done with the help of the function order().

order() is a function that gives you the ranked position of each element when it is applied on a variable, such as a vector for example:

a <- c(100, 10, 1000) order(a) [1] 2 1 3 10, which is the second element in a, is the smallest element, so 2 comes first in the output of order(a). 100, which is the first element in a is the second smallest element, so 1 comes second in the output of order(a).

This means we can use the output of order(a) to reshuffle a:

a[order(a)][1] 10 100 1000 INSTRUCTIONS 70 XP INSTRUCTIONS 70 XP Experiment with the order() function in the console. Click ‘Submit Answer’ when you are ready to continue.

Show Answer (-70 XP) HINT Just play with the order() function in the console!

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFBsYXkgYXJvdW5kIHdpdGggdGhlIG9yZGVyIGZ1bmN0aW9uIGluIHRoZSBjb25zb2xlIn0=

Sorting your data frame Alright, now that you understand the order() function, let us do something useful with it. You would like to rearrange your data frame such that it starts with the smallest planet and ends with the largest one. A sort on the diameter column.

INSTRUCTIONS 70 XP Call order() on planets_df\(diameter (the diameter column of planets_df). Store the result as positions. Now reshuffle planets_df with the positions vector as row indexes inside square brackets. Keep all columns. Simply print out the result. Show Answer (-70 XP) HINT Use order(planets_df\)diameter) to create positions. Now, you can use positions inside square brackets: planets_df[…]; can you fill in the …?
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIHBsYW5ldHNfZGYgaXMgcHJlLWxvYWRlZCBpbiB5b3VyIHdvcmtzcGFjZVxuXG4jIFVzZSBvcmRlcigpIHRvIGNyZWF0ZSBwb3NpdGlvbnNcbnBvc2l0aW9ucyA8LSBvcmRlcihwbGFuZXRzX2RmJGRpYW1ldGVyKVxuXG4jIFVzZSBwb3NpdGlvbnMgdG8gc29ydCBwbGFuZXRzX2RmXG5wbGFuZXRzX2RmW3Bvc2l0aW9ucywgXSJ9