Tutorials
r programming
+1

# 15 Easy Solutions To Your Data Frame Problems In R

Discover how to create a data frame in R, change column and row names, access values, attach data frames, apply functions and much more.

R data frames regularly create somewhat of a furor on public forums like Stack Overflow and Reddit. Starting R users often experience problems with this particular data structure and it doesn’t always seem to be straightforward. But does it really need to be so?

Well, not necessarily.

With today’s post, DataCamp wants to show you that these R data structures don’t need to be hard: we offer you 15 easy, straightforward solutions to the most frequently occuring problems with data.frame. These issues have been selected from the most recent and sticky or upvoted Stack Overflow posts.

(To practice data frames in R, try the data frame chapter of DataCamp's introduction to R course.)

## The Root: What’s An R Data Frame Exactly?

With the data frame, R offers you a great first step by allowing you to store your data in overviewable, rectangular grids. Each row of these grids corresponds to measurements or values of an instance, while each column is a vector containing data for a specific variable.

This means that a data frame’s rows do not need to contain, but can contain, the same type of values: they can be numeric, character, logical, etc.;

As you can see below, each instance, listed in the first unnamed column with a number, has certain characteristics that are spread out over the remaining three columns. Each column needs to consist of values of the same type, since they are data vectors: as such, the breaks column only contains numerical values, while the wool and tension columns have characters as values that are stored as factors.

In case you’re wondering, this data is about the number of breaks in yarn during weaving :).

Remember that factors are variables that can only contain a limited number of different values. As such, they are often called categorical variables.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJoZWFkKHdhcnBicmVha3MpIn0=

Maybe you will have already noticed that this data structure ressembles that of matrices, except for the fact that their data values don’t need to be of the same type, while matrices do require this.

Data frames also have similarities with lists, which are basically collections of components. However, it’s a list with vector structures of the same length. As such, they can actually be seen as special types of lists and can be accessed as either a matrix or a list.

If you want more information or if you just want to review and take a look at a comparison of the five general data structures in R, watch the small video below:

As you can see, there are different data structures that impose different requirements on how the data is stored. Data frames are particularly handy to store multiple data vectors, which makes it easier to organize your data, to apply functions to it and to save your work.

It’s almost similar to having a single spreadsheet with elements that all have equal lengths!

## The Basics: Questions And Solutions

### How To Create A Simple Data Frame in R

Even though looking at built-in examples of this data structure, such as esoph, is interesting, it can easily get more exciting!

How?

By practising with your own examples, of course! You can do this very easily by making some vectors first:

Died.At <- c(22,40,72,41)
Writer.At <- c(16, 18, 36, 36)
First.Name <- c("John", "Edgar", "Walt", "Jane")
Second.Name <- c("Doe", "Poe", "Whitman", "Austen")
Sex <- c("MALE", "MALE", "MALE", "FEMALE")
Date.Of.Death <- c("2015-05-10", "1849-10-07", "1892-03-26","1817-07-18")

Next, you just combine the vectors that you made with the data.frame() function:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkRpZWQuQXQgPC0gYygyMiw0MCw3Miw0MSlcbldyaXRlci5BdCA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuRmlyc3QuTmFtZSA8LSBjKFwiSm9oblwiLCBcIkVkZ2FyXCIsIFwiV2FsdFwiLCBcIkphbmVcIilcblNlY29uZC5OYW1lIDwtIGMoXCJEb2VcIiwgXCJQb2VcIiwgXCJXaGl0bWFuXCIsIFwiQXVzdGVuXCIpXG5TZXggPC0gYyhcIk1BTEVcIiwgXCJNQUxFXCIsIFwiTUFMRVwiLCBcIkZFTUFMRVwiKVxuRGF0ZS5PZi5EZWF0aCA8LSBjKFwiMjAxNS0wNS0xMFwiLCBcIjE4NDktMTAtMDdcIiwgXCIxODkyLTAzLTI2XCIsXCIxODE3LTA3LTE4XCIpIiwic2FtcGxlIjoiIyBNYWtlIHRoZSBkYXRhZnJhbWUgd2l0aCB0aGUgaGVscCBvZiBkYXRhLmZyYW1lKClcbndyaXRlcnNfZGYgPC0gLi4uLi4uLi4uLihEaWVkLkF0LCBXcml0ZXIuQXQsIEZpcnN0Lk5hbWUsIFNlY29uZC5OYW1lLCBTZXgsIERhdGUuT2YuRGVhdGgpXG5cbiMgVXNlIHN0cigpIHRvIGdldCBtb3JlIGluZm8gYWJvdXQgYHdyaXRlcnNfZGZgXG4uLi4od3JpdGVyc19kZikiLCJzb2x1dGlvbiI6IiMgTWFrZSB0aGUgZGF0YWZyYW1lIHdpdGggdGhlIGhlbHAgb2YgZGF0YS5mcmFtZSgpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoRGllZC5BdCwgV3JpdGVyLkF0LCBGaXJzdC5OYW1lLCBTZWNvbmQuTmFtZSwgU2V4LCBEYXRlLk9mLkRlYXRoKVxuXG4jIFVzZSBzdHIoKSB0byBnZXQgbW9yZSBpbmZvIGFib3V0IGB3cml0ZXJzX2RmYFxuc3RyKHdyaXRlcnNfZGYpIiwic2N0IjoidGVzdF9mdW5jdGlvbihcImRhdGEuZnJhbWVcIiwgbm90X2NhbGxlZF9tc2cgPSBcIkRpZCB5b3UgdXNlIGBkYXRhLmZyYW1lKClgIHRvIG1ha2UgeW91ciBkYXRhIGZyYW1lP1wiKVxudGVzdF9vYmplY3QoXCJ3cml0ZXJzX2RmXCIsIGluY29ycmVjdF9tc2c9XCJBcmUgeW91IHN1cmUgdGhhdCB5b3UgaW5jbHVkZWQgYWxsIHZlY3RvcnMgaW4gdGhlIGRhdGEgZnJhbWU/XCIpXG50ZXN0X2Z1bmN0aW9uKFwic3RyXCIsIFwib2JqZWN0XCIsIG5vdF9jYWxsZWRfbXNnID0gXCJEaWQgeW91IHVzZSBgc3RyKClgIHRvIGdldCB0byBrbm93IG1vcmUgYWJvdXQgd3JpdGVyc19kZj9cIiwgaW5jb3JyZWN0X21zZz1cIkRpZCB5b3Ugc3BlY2lmeSBgd3JpdGVyc19kZmAgYXMgYW4gYXJndW1lbnQgb2YgYHN0cigpYD9cIilcbnRlc3RfZXJyb3IoKVxuc3VjY2Vzc19tc2coXCJXZWxsIGRvbmUhIFlvdSBhcmUgd2VsbCBvbiB5b3VyIHdheSB0byBiZWNvbWluZyBhbiBleHBlcnQgaW4gUiBkYXRhIGZyYW1lcyFcIikifQ==

Remember that this type of data structure requires variables of the same length. Check if you have put an equal number of arguments in all c() functions that you assign to the vectors and that you have indicated strings of words with "".

Also, note that when you use the data.frame() function, character variables are imported as factors or categorical variables. Use the str() function to get to know more about writers_df.

However, if you’re more interested in inspecting the first and the last lines of writers_df, you can use the head() and tail() funtions, respectively.

You see that the First.Name, Second.Name, Sex and Date.Of.Death variables of writers_df have all been read in as factors.

But do you really want this?

• For the variables First.Name and Second.Name, you don’t want this. You can use the I() function to insulate them. This function inhibits the interpretation of its arguments. In other words, by just slightly changing the definitions of the vectors First.Name and Second.Name with the addition of the I() function, you can make sure that the proper names are not interpreted as factors.

• You can keep the Sex vector as a factor, because there are only a limited amount of possible values that this variable can have.

• Also for the variable Date.of.Death you don’t want to have a factor. It would be better if the values are registered as dates. You can add the as.Date() function to this variable to make sure this happens.

If you use other functions such as read.table() or other functions that are used to input data, such as read.csv() and read.delim(), you’ll get back a data frame as the result. This way, files that look like this one below or files that have other delimiters, will be converted once they are read into R with these functions.

22, 16, John, Doe, MALE, 2015-05-10
40, 18, Edgar, Poe, MALE, 1849-10-07
72, 36, Walt, Whitman, MALE, 1892-03-26
41, 36, Jane, Austen, FEMALE, 1817-07-18

If you want to know more about how you can read and import Excel files into R, make sure to check out our tutorial! Alternatively, you could check out the Rdocumentation page on read.table.

### How To Change A Data Frame’s Row And Column Names

Data frames can also have a names attribute, by which you can see the names of the variables that you have included. In other words, you can also set a header.

You already did this before when making writers_df;

You see that the names of the variables Died.At, Writer.At, First.Name, Second.Name, Sex and Date.Of.Death appear:

Now that you see the names of writers_df, you’re not so sure if these are efficient or correct. To change the names that appear, you can easily continue using the names() function.

Make sure, though, that you have a number of arguments in the c() function that is equal to the number of variables that you have included into writers_df.

In this case, since there are six variables Died.At, Writer.At, First.Name, Second.Name, Sex and Death, you want six arguments in the c() function.

Otherwise, the other variables will be interpreted as “NA”.

Note also how the arguments of the c() function are inputted as strings!

Tip: try to leave out the two last arguments from the c() function and see what happens!

Note that you can also access and change the column and row names with the functions colnames() and rownames(), respectively:

As you already know, this data structure has similarities to matrices; This means that the size is determined by how many rows and columns you have combined into it.

To check how many rows and columns you have in writers_df, you can use the dim() function:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5E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ID0gMyxcbiAgICAgICAgICAgICAgbm90X2NhbGxlZF9tc2cgPSAgXCJEaWQgeW91IHVzZSB0aGUgYGRpbSgpYCBmdW5jdGlvbiB0byByZXRyaWV2ZSB0aGUgbnVtYmVyIG9mIGNvbHVtbnMgb2YgeW91ciBkYXRhIGZyYW1lP1wiLFxuICAgICAgICAgICAgICBhcmdzX25vdF9zcGVjaWZpZWQgPSBcIkRpZCB5b3UgcGFzcyBgd3JpdGVyc19kZmAgdG8gYGRpbSgpYD9cIixcbiAgICAgICAgICAgICAgaW5jb3JyZWN0X21zZyA9IFwiQXJlIHlvdSBzdXJlIHlvdSBpbmNsdWRlZCBgd3JpdGVyc19kZmAgYXMgYW4gYXJndW1lbnQgZm9yIGBkaW0oKWA/XCIpXG50ZXN0X2Vycm9yKClcbnN1Y2Nlc3NfbXNnKFwiQW1hemluZyFcIikifQ==

The result of this function is represented as [1] 4 6. Just like a matrix, the dimensions are defined by the number of rows, followed by the number of columns.

Note that you can also just retrieve the number of rows or columns by adding a [] with an index to your dim() function.

You can also retrieve the number of rows and columns writers_df by using the functions nrow() and ncol(), to retrieve the number of rows or columns, respectively:

Note that, since the data structure is also similar to a list, you could also use the length() function to retrieve the number of columns.

### How To Access And Change A Data Frame’s Values

There are two main ways in which you can access and change these values. In this section, you’ll see and practice both of them!

#### …. Through The Variable Names

Now that you have retrieved and set the names of writers_df, you want to take a closer look at the values that are actually stored in it.

There are two straightforward ways that you can access these values.

First, you can try to access them by just entering the data frame’s name in combination with the variable name:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lX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SB5b3UgaGF2ZW4ndCByZXR1cm5lZCB0aGUgYHdyaXRlcnNfZGZgIHlldCFcIilcbnRlc3RfZXJyb3IoKVxuc3VjY2Vzc19tc2coXCJHb29kIGpvYiFcIikifQ==

Note that if you change one of the values in the vector Age that this change will not be incorporated into writers_df.

In the end, with this method of accessing the values, you just create a copy of a certain variable!

That’s why any changes to the variables do not change the data frame’s variables.

writers_df$Age.At.Death Note also that you can also change the values by simply using these notations to perform mathematical operations. eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoQWdlLkF0LkRlYXRoLCBBZ2UuQXMuV3JpdGVyLCBOYW1lLCBTdXJuYW1lLCBHZW5kZXIsIERlYXRoKSIsInNhbXBsZSI6IiMgU3VidHJhY3QgMSB0byBhbGwgdmFsdWVzIGluIGBBZ2UuQXQuRGVhdGhgIFxud3JpdGVyc19kZiRBZ2UuQXQuRGVhdGggPC0gLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4tLlxuXG4jIEdpdmUgYmFjayBgd3JpdGVyc19kZmBcbi4uLi4uLi4uLiIsInNvbHV0aW9uIjoiIyBTdWJ0cmFjdCAxIHRvIGFsbCB2YWx1ZXMgaW4gYEFnZS5BdC5EZWF0aGAgXG53cml0ZXJzX2RmJEFnZS5BdC5EZWF0aCA8LSB3cml0ZXJzX2RmJEFnZS5BdC5EZWF0aC0xXG5cbiMgR2l2ZSBiYWNrIGB3cml0ZXJzX2RmYFxud3JpdGVyc19kZiIsInNjdCI6InRlc3RfZGF0YV9mcmFtZShcIndyaXRlcnNfZGZcIiwgXCJBZ2UuQXQuRGVhdGhcIilcbnRlc3Rfb3V0cHV0X2NvbnRhaW5zKFwid3JpdGVyc19kZlwiLCBcbiAgICAgICAgICAgICAgICAgICAgIGluY29ycmVjdF9tc2c9XCJPb3BzLCBsb29rcyBsaWtlIHlvdSBoYXZlbid0IHJldHVybmVkIHRoZSBgd3JpdGVyc19kZmAgeWV0IVwiKVxudGVzdF9lcnJvcigpXG5zdWNjZXNzX21zZyhcIkdvb2Qgam9iIVwiKSJ9 If you really want to make your hands dirty some more and change some of the values of writers_df, you can use the [,] notation to actually change the values one by one: eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZn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Rlc3RfZGF0YV9mcmFtZShcIndyaXRlcnNfZGZcIiwgXCJHZW5kZXJcIiwgaW5jb3JyZWN0X21zZz1cIkRpZCB5b3UgcGFzcyBgRkVNQUxFYCBhcyB0aGUgbmV3IHZhbHVlIGZvciB0aGUgZmlyc3Qgcm93LCBmaWZ0aCBjb2x1bW4gb2YgYHdyaXRlcnNfZGZgP1wiKVxudGVzdF9vdXRwdXRfY29udGFpbnMoXCJ3cml0ZXJzX2RmXCIsIFxuICAgICAgICAgICAgICAgICAgICAgaW5jb3JyZWN0X21zZz1cIk9vcHMsIGxvb2tzIGxpa2UgeW91IGhhdmVuJ3QgcmV0dXJuZWQgdGhlIGB3cml0ZXJzX2RmYCB5ZXQhXCIpXG50ZXN0X2Vycm9yKClcbnN1Y2Nlc3NfbXNnKFwiR29vZCBqb2IhXCIpIn0= ### Why And How To Attach Data Frames The $ notation is pretty handy, but it can become very annoying when you have to type it each time that you want to work with your data.

The attach() function offers a solution to this: it takes a data frame as an argument and places it in the search path at position 2.

So unless there are variables in position 1 that are exactly the same as the ones from the data frame that you have inputted, the variables are considered as variables that can be immediately called on.

Note that the search path is in fact the order in which R accesses files. You can look this up by entering the search() function.

Note that you can alternatively use the with() function to attach writers_df, but this requires you to specify some more arguments.

You get an error that tells you that “The following objects are masked by .GlobalEnv:”.

This is because you have objects in your global environment that have the same name as your data frame. Those objects could be the vectors that you created above, if you didn’t change their names.

You have two solutions to this:

1. You just don’t create any objects with those names in your global environment. This is more a solution for those of you who imported their data through read.table(), read.csv() or read.delim(), but not really appropriate for this case.
2. You rename the objects in the data frame so that there’s no conflict. This is the solution that was applied in this tutorial. So, rename your columns with the names() or colnames() functions.

Note that if all else fails, you can just remember to always refer to your column names with the $notation! Now that you have unmasked the object, you can now safely execute the following command and you can actually access/change the values of all writers_df’s variables: Age.At.Death <- Age.At.Death-1 Age.At.Death ### How To Apply Functions To Data Frames Now that you have successfully made and modified writers_df by putting a header in place, you can start applying functions to it! In some cases where you want to calculate stuff,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UgaGF2ZW4ndCByZXR1cm5lZCB0aGUgYEFnZXNgIHlldCFcIilcbnRlc3RfZXJyb3IoKVxuc3VjY2Vzc19tc2coXCJXZWxsIGRvbmUhIFRoZXNlIGFyZSB0aGUgZmlyc3Qgc3RlcHMgdG8gYXBwbHlpbmcgZnVuY3Rpb25zIG9uIHlvdXIgZGF0YSBmcmFtZSFcIikifQ== Only then can you start to get, for example, the mean and the median of your numeric data. You can do this with the apply() function. The first argument of this function should be your smaller data frame, in this case, Ages. The second argument designates what data you want to consider for the calculations of the mean or median: columns or rows. In this case, you want to calculate the median and mean of the variables Age.At.Death and Age.As.Writer, which designate columns in writers_df. The last argument then specifies the exact calculations that you want to do on your data: eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IndyaXRlcnNfZGYgPC0gZGF0YS5mcmFtZShjKDIyLDQwLDcyLDQxKSwgYygxNiwgMTgsIDM2LCAzNiksIEkoYyhcIkpvaG5cIiwgXCJFZGdhclwiLCBcIldhbHRcIiwgXCJKYW5lXCIpKSwgSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSksIGMoXCJNQUxFXCIsIFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJGRU1BTEVcIiksIGFzLkRhdGUoYyhcIjIwMTUtMDUtMTBcIiwgXCIxODQ5LTEwLTA3XCIsIFwiMTg5Mi0wMy0yNlwiLFwiMTgxNy0wNy0xOFwiKSkpXG5BZ2VzIDwtIHdyaXRlcnNfZGZbLDE6Ml0iLCJzYW1wbGUiOiIjIEFwcGx5IHRoZSBtZWRpYW4gdG8gYEFnZXNgXG5hcHBseShBZ2VzLCAyLCAuLi4uLi4pXG5hcHBseShBZ2VzLCAxLCBtZWRpYW4pXG5cbiMgb3IgeW91IGNhbiBhcHBseSB0aGUgbWVhbiB0byBgQWdlc2BcbmFwcGx5KEFnZXMsIDIsIC4uLi4pIiwic29sdXRpb24iOiIjIEFwcGx5IH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VuY3Rpb24/XCIsXG4gICAgICAgICAgICAgIGluY29ycmVjdF9tc2c9XCJBcmUgeW91IHN1cmUgeW91IHBhc3NlZCBhbGwgYXJndW1ldG5zIHRvIGBhcHBseSgpYD9cIilcbnRlc3RfZnVuY3Rpb24oXCJhcHBseVwiLFxuICAgICAgICAgICAgICBhcmdzID0gYyhcIlhcIiwgXCJNQVJHSU5cIiwgXCJGVU5cIiksXG4gICAgICAgICAgICAgIDMsXG4gICAgICAgICAgICAgIG5vdF9jYWxsZWRfbXNnPVwiRGlkIHlvdSB1c2UgdGhlIGBhcHBseSgpYCBmdW5jdGlvbiB0byBhcHBseSB0aGUgbWVhbiB0byBgQWdlc2A/XCIsXG4gICAgICAgICAgICAgIGFyZ3Nfbm90X3NwZWNpZmllZCA9IFwiSGF2ZSB5b3UgcGFzc2VkIGBtZWFuYCB0byBgYXBwbHkoKWA/XCIsXG4gICAgICAgICAgICAgIGluY29ycmVjdF9tc2c9XCJBcmUgeW91IHN1cmUgeW91IHBhc3NlZCBhbGwgYXJndW1lbnRzIHRvIGBhcHBseSgpYD9cIilcbnN1Y2Nlc3NfbXNnKFwiQW1hemluZyFcIikifQ== Do you want to know more about the apply() function and how to use it? DataCamp’s Intermediate R course, which teaches you, amongst other things, how to make your R code more efficient and readable using this function, along with the rest of the apply() family of functions. ## Surpassing The Basics: More Questions, More Answers Now that you have been introduced to the basic pitfalls, it’s time to look at some problems, questions or difficulties that you might have already had while working with these data structures more intensively. If you’re new to this topic, the following section will allow you to step up your data frame game. All the more reason to get started now! ### How To Create An Empty Data Frame The easiest way to create an empty data frame is probably by just assigning a data.frame() function without any arguments to a vector: eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJhYiA8LSBkYXRhLmZyYW1lKClcbmFiIn0= You can then start filling your ab up by using the [,] notation. Be careful, however, because it’s easy to make errors while doing this! Note how you don’t see any column names in this empty data set. If you do want to have those, you can just initialize empty vectors in ab, like this: eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImFiIDwtIGRhdGEuZnJhbWUoKSIsInNhbXBsZSI6IkFnZSA8LSBudW1lcmljKClcbk5hbWUgPC0gY2hhcmFjdGVyKClcbklEIDwtIGludGVnZXIoKVxuR2VuZGVyIDwtIGZhY3RvcigpXG5EYXRlIDwtIGFzLkRhdGUoY2hhcmFjdGVyKCkpXG5hYiA8LSBkYXRhLmZyYW1lKGMoQWdlLCBOYW1lLCBJRCwgR2VuZGVyLCBEYXRlKSlcbmFiIn0= ### How To Extract Rows And Columns, Subsetting Your Data Frame Subsetting or extracting specific rows and columns is an important skill in order to surpass the basics that have been introduced in step two, because it allows you to easily manipulate smaller sets of your original data. You basically extract those values from the rows and columns that you need in order to optimize the data analyses you make. It’s easy to start subsetting with the [,] notation that was described in step two: eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoQWdlLkF0LkRlYXRoLCBBZ2UuQXMuV3JpdGVyLCBOYW1lLCBTdXJuYW1lLCBHZW5kZXIsIERlYXRoKSIsInNhbXBsZSI6IiMgU3Vic2V0IHRoZSBgd3JpdGVy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ICAgICAgICAgICAgICAgIGluY29ycmVjdF9tc2c9XCJPb3BzLCBsb29rcyBsaWtlIHlvdSBoYXZlbid0IHJldHVybmVkIGB3cml0ZXJfbmFtZXNfZGZgIHlldCFcIilcbnRlc3Rfb2JqZWN0KFwid3JpdGVyX25hbWVzX2RmMlwiLCBpbmNvcnJlY3RfbXNnPVwiQXJlIHlvdSBzdXJlIHRoYXQgeW91IHN1YnNldHRlZCBgd3JpdGVyc19kZmAgY29ycmVjdGx5P1wiKVxudGVzdF9vdXRwdXRfY29udGFpbnMoXCJ3cml0ZXJfbmFtZXNfZGYyXCIsIHRpbWVzPTIsXG4gICAgICAgICAgICAgICAgICAgICBpbmNvcnJlY3RfbXNnPVwiT29wcywgbG9va3MgbGlrZSB5b3UgaGF2ZW4ndCByZXR1cm5lZCBgd3JpdGVyX25hbWVzX2RmYCB5ZXQhXCIpXG50ZXN0X2Vycm9yKClcbnN1Y2Nlc3NfbXNnKFwiV2VsbCBkb25lISBLZWVwIG9uIGdvaW5nIHRvIGtub3cgbW9yZSBhYm91dCBzdWJzZXR0aW5nIHlvdXIgZGF0YSBmcmFtZXMuXCIpIn0= Note that you can also define this subset with the variable names. Tip: be careful when you are subsetting just one column! R has the tendency to simplify your results, which means that it will read your subset as a vector, which normally, you don’t want to get. To make sure that this doesn’t happen, you can add the argument drop=FALSE: eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQU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0bHkgc3Vic2V0IGB3cml0ZXJzX2RmYD9cIilcbnRlc3Rfb3V0cHV0X2NvbnRhaW5zKFwiamFuZV93cml0ZXJzX2RmXCIsIGluY29ycmVjdF9tc2c9XCJEaWQgeW91IHJldHVybiBgamFuZV93cml0ZXJzX2RmYD9cIilcbnRlc3Rfb2JqZWN0KFwiZm9ydHlfd3JpdGVyc19kZlwiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSBjb3JyZWN0bHkgc3Vic2V0IGB3cml0ZXJzX2RmYD9cIilcbnRlc3Rfb3V0cHV0X2NvbnRhaW5zKFwiZm9ydHlfd3JpdGVyc19kZlwiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSByZXR1cm4gYGZvcnR5X3dyaXRlcnNfZGZgP1wiKVxudGVzdF9lcnJvcigpXG5zdWNjZXNzX21zZyhcIkdvb2Qgam9iIVwiKSJ9 Note that you can also turn to grep() to subset. In the DataCamp Light chunk above, you used grep() to get the job done. You isolated the rows in the column Age.At.Death that have values that contain “4”. Note that by subsetting, you basically stop considering certain values. This might mean that you remove certain features of a factor, by, for example, only considering the MALE members of writers_df. Notice how all factor levels of this column still remain present, even though you have created a subset: eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0O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CBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSBwYXNzIGBHZW5kZXJgIHRvIGBmYWN0b3IoKWA/XCIpXG50ZXN0X2Vycm9yKClcbnN1Y2Nlc3NfbXNnKFwiR29vZCBqb2IhXCIpIn0= You can use factor() to remove the factor levels that are no longer present, you can enter the following line of code. ### How To Remove Columns And Rows From A Data Frame If you want to remove values or entire columns, you can assign a NULL value to the desired unit: eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoQWdlLkF0LkRlYXRoLCBBZ2UuQXMuV3JpdGVyLCBOYW1lLCBTdXJuYW1lLCBHZW5kZXIsIERlYXRoKSIsInNhbXBsZSI6IiMgQXNzaWduIGBOVUxMYCB0byB0aGUgdmFsdWUgaW4gWzEsM11cbndyaXRlcnNfZGZbMSwzXSA8LSBOVUxMXG5cbiMgQXNzaWduIGBOVUxMYCBUTyBgQWdlLkF0LkRlYXRoYFxuQWdlLkF0LkRlYXRoIDwtIE5VTExcblxuIyBQcmludCBvdXQgYHdyaXRlcnNfZGZgXG53cml0ZXJzX2RmIn0= To remove rows, the procedure is a bit more complicated. You define a new vector in which you list for every row whether to have it included or not. Then, you apply this vector to writers_df: eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoQWdlLkF0LkRlYXRoLCBBZ2UuQXMuV3JpdGVyLCBOYW1lLCBTdXJuYW1lLCBHZW5kZXIsIERlYXRoKSIsInNhbXBsZSI6IiMgRGVmaW5lIHRoZSByb3dzIHlvdSB3YW50IHRvIGtlZXAgXG5yb3dzX3RvX2tlZXAgPC0gYyhUUlVFLCBGQUxTRSwgVFJVRSwgRkFMU0UpXG5cbiMgU3Vic2V0IGB3cml0ZXJzX2RmYCB3aXRoIHRoZSBgcm93c190b19rZWVwYFxubGltaXRlZF93cml0ZXJzX2RmIDwtIHdyaXRlcnNfZGZbLi4uLi4uLi4uLi4sXVxuXG4jIFJldHVybiBgbGltaXRlZF93cml0ZXJzX2RmYFxubGltaXRlZF93cml0ZXJzX2RmXG5cbiMgU3Vic2V0IGB3cml0ZXJzX2RmYCB3aXRoIHRoZSByb3dzIG5vdCB0byBrZWVwXG5sZXNzX3dyaXRlcnNfZGYgPC0gd3JpdGVy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m9ydHlfc3RoX3dyaXRlcnNcIiwgaW5jb3JyZWN0X21zZz1cIkRpZCB5b3UgcmV0dXJuIGBmb3J0eV9zdGhfd3JpdGVyc2A/XCIpXG50ZXN0X2Vycm9yKClcbnN1Y2Nlc3NfbXNnKFwiQXdlc29tZSFcIikifQ== Note that you can also do the opposite by just adding !, stating that the reverse is true. Also note that you can also work with tresholds. In the code chunk above, you specified that you only want to keep all writers that were older than forty when they died. ### How To Add Rows And Columns To A Data Frame Much in the same way that you used the [,] and$ notations to access and change single values, you can also easily add columns to writers_df:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoQWdlLkF0LkRlYXRoLCBBZ2UuQXMuV3JpdGVyLCBOYW1lLCBTdXJuYW1lLCBHZW5kZXIsIERlYXRoKSIsInNhbXBsZSI6IiMgQWRkIGEgYGxvY2F0aW9uYCBjb2x1bW4gdG8gYHdyaXRlcnNfZGZgIHdpdGggYGMoKWBcbndyaXRlcnNfZGYkTG9jYXRpb24gPC0gLihcIkJlbGdpdW1cIiwgXCJVbml0ZWQgS2luZ2RvbVwiLCBcIl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IVwiKSJ9

Appending rows to an existing data frame is somewhat more complicated.

To easily do this by first making a new row in a vector, respecting the column variables that have been defined in writers_df and by then binding this row to the original data frame with the rbind() funtion:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoQWdlLkF0LkRlYXRoLCBBZ2UuQXMuV3JpdGVyLCBOYW1lLCBTdXJuYW1lLCBHZW5kZXIsIERlYXRoKSIsInNhbXBsZSI6IiMgRGVmaW5lIGEgbmV3IHJvd1xubmV3X3JvdyA8LSAuKDUwLCAyMiwgXCJSb2JlcnRvXCIsIFwiQm9sYW5vXCIsIFwiTUFMRVwiLCBcIjIwMDMtMDctMTVcIilcblxuIyBCaW5kIHRoZSBuZXcgcm93IHRvIGB3cml0ZXJzX2RmYCB3aXRoIGByYmluZCgpYFxud3JpdGVyc19kZl9sYXJnZSA8LSAuLi4uLih3cml0ZXJzX2RmLCBuZXdfcm93KVxuXG4jIFJldHVybiBgd3JpdGVyc19kZl9sYXJnZWBcbi4uLi4uLi4uLi4uLi4uLi4iLCJzb2x1dGlvbiI6IiMgRGVmaW5lIGEgbmV3IH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In0=

### Why And How To Reshape An R Data Frame From Wide To Long Format And Vice Versa

When you have multiple values, spread out over multiple columns, for the same instance, your data is in the “wide” format.

On the other hand, when your data is in the “long” format if there is one observation row per variable. You therefore have multiple rows per instance.

Let’s illustrate this with an example. Long data looks like this:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERlZmluZSByb3dzIFxuU3ViamVjdCA8LSBjKDEsMiwxLDIsMiwxKVxuR2VuZGVyIDwtIGMoXCJNXCIsIFwiRlwiLCBcIk1cIiwgXCJGXCIsIFwiRlwiLFwiTVwiKVxuVGVzdCA8LSBjKFwiUmVhZFwiLCBcIldyaXRlXCIsIFwiV3JpdGVcIiwgXCJMaXN0ZW5cIiwgXCJSZWFkXCIsIFwiTGlzdGVuXCIpXG5SZXN1bHQgPC0gYygxMCwgNCwgOCwgNiwgNywgNylcblxuIyBNYWtlIGBvYnNlcnZhdGlvbnNfbG9uZ2Agd2l0aCBgZGF0YS5mcmFtZSgpYFxub2JzZXJ2YXRpb25zX2xvbmcgPC0gLi4uLi4uLi4uLi4oU3ViamVjdCwgR2VuZGVyLCBUZXN0LCBSZXN1bHQpXG5cbiMgUmV0dXJuIGBvYnNlcnZhdGlvbnNfbG9uZ2Bcbi4uLi4uLi4uLi4uLi4uLi4uLiIsInNvbHV0aW9uIjoiIyBEZWZpbmUgcm93cyBcblN1YmplY3QgPC0gYygxLDIsMSwyLDIsMSlcbkdlbmRlciA8LSBjKFwiTVwiLCBcIkZcIiwgXCJNXCIsIFwiRlwiLCBcIkZcIixcIk1cIilcblRlc3QgPC0gYyhcIlJlYWRcIiwgXCJXcml0ZVwiLCBcIldyaXRlXCIsIFwiTGlzdGVuXCIsIFwiUmVhZFwiLCBcIkxpc3RlblwiKVxuUmVzdWx0IDwtIGMoMTAsIDQsIDgsIDYsIDcsIDcpXG5cbiMgTWFrZSBgb2JzZXJ2YXRpb25zX2xvbmdgIHdpdGggYGRhdGEuZnJhbWUoKWBcbm9ic2VydmF0aW9uc19sb25nIDwtIGRhdGEuZnJhbWUoU3ViamVjdCwgR2VuZGVyLCBUZXN0LCBSZXN1bHQpXG5cbiMgUmV0dXJuIGBvYnNlcnZhdGlvbnNfbG9uZ2Bcbm9ic2VydmF0aW9uc19sb25nIiwic2N0IjoidGVzdF9vYmplY3QoXCJTdWJqZWN0XCIpXG50ZXN0X29iamVjdChcIkdlbmRlclwiKVxudGVzdF9vYmplY3QoXCJUZXN0XCIpXG50ZXN0X29iamVjdChcIlJlc3VsdFwiKVxudGVzdF9vYmplY3QoXCJvYnNlcnZhdGlvbnNfbG9uZ1wiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSBtYWtlIGEgZGF0YSBmcmFtZSB3aXRoIGBkYXRhLmZyYW1lKClgP1wiLCB1bmRlZmluZWRfbXNnPVwiRGlkIHlvdSBtYWtlIGEgZGF0YSBmcmFtZSB3aXRoIGBkYXRhLmZyYW1lKClgP1wiKVxudGVzdF9vdXRwdXRfY29udGFpbnMoXCJvYnNlcnZhdGlvbnNfbG9uZ1wiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSByZXR1cm4gdGhlIGRhdGEgZnJhbWUgYG9ic2VydmF0aW9uc19sb25nYD9cIilcbnRlc3RfZXJyb3IoKVxuc3VjY2Vzc19tc2coXCJHb29kIGpvYiEgTm93IHN0dWR5IHdoYXQgZGF0YSBpbiBhIGxvbmcgZm9ybWF0IGxvb2tzIGxpa2UuXCIpIn0=

As you can see, there is one row for each value that you have in the Type variable. A lot of statistical tests favor this format.

The data would look like the following in the wide format:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERlZmluZSBjb2x1bW5zXG5TdWJqZWN0IDwtIGMoMSwyKVxuR2VuZGVyIDwtIGMoXCJNXCIsIFwiRlwiKVxuUmVhZCA8LSBjKDEwLCA3KVxuV3JpdGUgPC1jKDgsIDQpXG5MaXN0ZW4gPC0gYyg3LCA2KVxuXG4jIE1ha2UgYG9ic2VydmF0aW9uc193aWRlYFxub2JzZXJ2YXRpb25zX3dpZGUgPC0gZGF0YS5mcmFtZShTdWJqZWN0LCBHZW5kZXIsIFJlYWQsIFdyaXRlLCBMaXN0ZW4pXG5cbiMgUmV0dXJuIGBvYnNlcnZhdGlvbnNfd2lkZWBcbm9ic2VydmF0aW9uc193aWRlIn0=

You see that each column represents a unique pairing of the various factors with the values.

Since different functions may require you to input your data either in “long” or “wide” format, you might need to reshape your data set.

There are two main options that you can choose here: you can use the stack() function or you can try using the reshape() function.

The former is preferred when you work with simple data frames, while the latter is more often used on more complex ones, mostly because there’s a difference in the possibilities that both functions offer.

Make sure to keep on reading to know more about the differences in possibilities between the stack() and reshape() functions!

#### Using stack() For Simply Structured Data Frames

The stack() function basically concatenates or combines multiple vectors into a single vector, along with a factor that indicates where each observation originates from.

To go from wide to long format, you will have to stack your observations, since you want one observation row per variable, with multiple rows per variable.

In this case, you want to merge the columns Read, Write and Listen together, qua names and qua values:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlN1YmplY3QgPC0gYygxLDIpXG5HZW5kZXIgPC0gYyhcIk1cIiwgXCJGXCIpXG5SZWFkIDwtIGMoMTAsIDcpXG5Xcml0ZSA8LWMoOCwgNClcbkxpc3RlbiA8LSBjKDcsIDYpXG5vYnNlcnZhdGlvbnNfd2lkZSA8LSBkYXRhLmZyYW1lKFN1YmplY3QsIEdlbmRlciwgUmVhZCwgV3JpdGUsIExpc3RlbikiLCJzYW1wbGUiOiIjIENvbnN0cnVjdCB0aGUgbG9uZyBmb3JtYXRcbmxvbmdfZm9ybWF0IDwtIC4uLi4uKG9ic2VydmF0aW9uc193aWRlLCBcbiAgICAgICAgICAgICAgICAgICAgIHNlbGVjdD1jKFJlYWQsIFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgV3JpdGUsIFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgTGlzdGVuKSlcblxuIyBSZXR1cm4gYGxvbmdfZm9ybWF0YFxuLi4uLi4uLi4uLi4iLCJzb2x1dGlvbiI6IiMgQ29uc3RydWN0IHRoZSBsb25nIGZvcm1hdFxubG9uZ19mb3JtYXQgPC0gc3RhY2sob2JzZXJ2YXRpb25zX3dpZGUsIFxuICAgICAgICAgICAgICAgICAgICAgc2VsZWN0PWMoUmVhZCwgXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgICBXcml0ZSwgXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgICBMaXN0ZW4pKVxuXG4jIFJldHVybiBgbG9uZ19mb3JtYXRgXG5sb25nX2Zvcm1hdCIsInNjdCI6InRlc3Rfb2JqZWN0KFwibG9uZ19mb3JtYXRcIiwgaW5jb3JyZWN0X21zZz1cIkRpZCB5b3UgdXNlIGBzdGFjaygpYCB0byBzdGFjayB0aGUgZGF0YSB0byBhIGxvbmcgZm9ybWF0P1wiLCB1bmRlZmluZWRfbXNnPVwiRGlkIHlvdSB1c2UgYHN0YWNrKClgIHRvIGdvIHRvIGEgbG9uZyBmb3JtYXQuXCIpXG50ZXN0X291dHB1dF9jb250YWlucyhcImxvbmdfZm9ybWF0XCIsIGluY29ycmVjdF9tc2c9XCJEaWQgeW91IHJldHVybiBgbG9uZ19mb3JtYXRgIGNvcnJlY3RseT9cIilcbnRlc3RfZXJyb3IoKVxuc3VjY2Vzc19tc2coXCJHcmVhdCBqb2IhXCIpIn0=

To go from long to wide format, you will need to unstack your data, which makes sense because you want to have one row per instance with each value present as a different variable.

Note here that you want to disentangle the Result and Test columns:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlN1YmplY3QgPC0gYygxLDIsMSwyLDIsMSlcbkdlbmRlciA8LSBjKFwiTVwiLCBcIkZcIiwgXCJNXCIsIFwiRlwiLCBcIkZcIixcIk1cIilcblRlc3QgPC0gYyhcIlJlYWRcIiwgXCJXcml0ZVwiLCBcIldyaXRlXCIsIFwiTGlzdGVuXCIsIFwiUmVhZFwiLCBcIkxpc3RlblwiKVxuUmVzdWx0IDwtIGMoMTAsIDQsIDgsIDYsIDcsIDcpXG5vYnNlcnZhdGlvbnNfbG9uZyA8LSBkYXRhLmZyYW1lKFN1YmplY3QsIEdlbmRlciwgVGVzdCwgUmVzdWx0KSIsInNhbXBsZSI6IiMgQ29uc3RydWN0IHRoZSB3aWRlIGZvcm1hdFxud2lkZV9mb3JtYXQgPC0gLi4uLi4uLihvYnNlcnZhdGlvbnNfbG9uZywgXG4gICAgICAgICAgICAgICAgICAgICAgIFJlc3VsdCB+IFRlc3QpXG5cbiMgUmV0dXJuIGB3aWRlX2Zvcm1hdGBcbi4uLi4uLi4uLi4iLCJzb2x1dGlvbiI6IiMgQ29uc3RydWN0IHRoZSB3aWRlIGZvcm1hdFxud2lkZV9mb3JtYXQgPC0gdW5zdGFjayhvYnNlcnZhdGlvbnNfbG9uZywgXG4gICAgICAgICAgICAgICAgICAgICAgIFJlc3VsdCB+IFRlc3QpXG5cbiMgUmV0dXJuIGB3aWRlX2Zvcm1hdGBcbndpZGVfZm9ybWF0Iiwic2N0IjoidGVzdF9vYmplY3QoXCJ3aWRlX2Zvcm1hdFwiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSB1c2UgYHVuc3RhY2soKWAgdG8gYnJpbmcgdGhlIGRhdGEgdG8gYSB3aWRlIGZvcm1hdD9cIiwgdW5kZWZpbmVkX21zZz1cIkRpZCB5b3UgdXNlIGB1bnN0YWNrKClgIHRvIGJyaW5nIHRoZSBkYXRhIHRvIGEgd2lkZSBmb3JtYXQ/XCIpXG50ZXN0X291dHB1dF9jb250YWlucyhcIndpZGVfZm9ybWF0XCIsIGluY29ycmVjdF9tc2c9XCJEaWQgeW91IHJldHVybiBgd2lkZV9mb3JtYXRgIGNvcnJlY3RseT9cIilcbnRlc3RfZXJyb3IoKVxuc3VjY2Vzc19tc2coXCJXZWxsIGRvbmUhXCIpIn0=

#### Using reshape() For Complex Data Frames

This function is part of the stats package. This function is similar to the stack() function, but is a little bit more elaborate. Read and see for yourself how reshaping your data works with the reshape() function:

To go from a wide to a long data format, you can first start off by entering the reshape() function.

The first argumnet should always be your original wide data set.

In this case, you can specify that you want to input the observations_wide to be converted to a long data format.

Then, you start adding other argumnets to the reshape() function:

1. Include a list of variable names that define the different measurements through varying. In this case, you store the scores of specific tests in the columns “Read”, “Write” and “Listen”.
2. Next, add the argumentv.names to specify the name that you want to give to the variable that contains these values in your long dataset. In this case, you want to combine all scores for all reading, writing and listening tests into one variable Score.
3. You also need to give a name to the variable that describes the different measurements that are inputted with the argument timevar. In this case, you want to give a name to the column that contains the types of tests that you give to your students. That’s why this column’s name should be called “Test”.
4. Then, you add the argument the argumenttimes, because you need to specify that the new column “Test” can only take three values, namely, the test components that you have stored: “Read”, “Write”, “Listen”.
5. You’re finally there! Give in the end format for the data with the argument direction.
6. Additionally, you can specify new row names with the argument new.row.names.

Tip: try leaving out this last argument and see what happens!

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlN1YmplY3QgPC0gYygxLDIsMSwyLDIsMSlcbkdlbmRlciA8LSBjKFwiTVwiLCBcIkZcIiwgXCJNXCIsIFwiRlwiLCBcIkZcIixcIk1cIilcblRlc3QgPC0gYyhcIlJlYWRcIiwgXCJXcml0ZVwiLCBcIldyaXRlXCIsIFwiTGlzdGVuXCIsIFwiUmVhZFwiLCBcIkxpc3RlblwiKVxuUmVzdWx0IDwtIGMoMTAsIDQsIDgsIDYsIDcsIDcpXG5vYnNlcnZhdGlvbnNfbG9uZyA8LSBkYXRhLmZyYW1lKFN1YmplY3QsIEdlbmRlciwgVGVzdCwgUmVzdWx0KSIsInNhbXBsZSI6IiMgSW1wb3J0IGByZXNoYXBlMmBcbmxpYnJhcnkoLi4uLi4uLi4uKVxuXG4jIENvbnZlcnQgdG8gYSB3aWRlIGZvcm1hdCB3aXRoIGBkY2FzdCgpYFxubG9uZ19yZXNoYXBlZDIgPC0gLi4uLi4ob2JzZXJ2YXRpb25zX2xvbmcsIFxuICA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CB0aW1lcz0xLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSByZXR1cm4gYGxvbmdfcmVzaGFwZWQyYCBjb3JyZWN0bHk/XCIpXG50ZXN0X2Vycm9yKClcbnN1Y2Nlc3NfbXNnKFwiV29vaG9vIVwiKSJ9

From long to wide, you take sort of the same steps. First, you take the reshape() function and give it its first argument, which is the data set that you want to reshape. The other arguments are as follows:

1. timevar allows you to specify that the variable Test, which describes the different tests that you give to your students, should be decomposed.
2. You also specify that the reshape() function shouldn’t take into account the variables Subject and Gender of the original data set. You put these column names into idvar.
3. By not naming the variable Result, the reshape() function will know that both Test and Result should be recombined.
4. You specify the direction of the reshaping, which is in this case, wide!

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlN1YmplY3QgPC0gYygxLDIsMSwyLDIsMSlcbkdlbmRlciA8LSBjKFwiTVwiLCBcIkZcIiwgXCJNXCIsIFwiRlwiLCBcIkZcIixcIk1cIilcblRlc3QgPC0gYyhcIlJlYWRcIiwgXCJXcml0ZVwiLCBcIldyaXRlXCIsIFwiTGlzdGVuXCIsIFwiUmVhZFwiLCBcIkxpc3RlblwiKVxuUmVzdWx0IDwtIGMoMTAsIDQsIDgsIDYsIDcsIDcpXG5vYnNlcnZhdGlvbnNfbG9uZyA8LSBkYXRhLmZyYW1lKFN1YmplY3QsIEdlbmRlciwgVGVzdCwgUmVzdWx0KSIsInNhbXBsZSI6IiMgUmVzaGFwZSB0byB3aWRlIGZvcm1hdFxud2lkZV9yZXNoYXBlIDwtIC4uLi4uLi4uKG9ic2VydmF0aW9uc19sb25nLCBcbiAgICAgICAgICAgICAgICAgICAgICAgIHRpbWV2YXIgPSBcIlRlc3RcIixcbiAgICAgICAgICAgICAgICAgICAgICAgIGlkdmFyID0gYyhcIlN1YmplY3RcIiwgXCJHZW5kZXJcIiksXG4gICAgICAgICAgICAgICAgICAgICAgICBkaXJlY3Rpb24gPSBcIndpZGVcIilcblxuIyBSZXR1cm4gYHdpZGVfcmVzaGFwZWBcbi4uLi4uLi4uLi4uIiwic29sdXRpb24iOiIjIFJlc2hhcGUgdG8gd2lkZSBmb3JtYXRcbndpZGVfcmVzaGFwZSA8LSByZXNoYXBlKG9ic2VydmF0aW9uc19sb25nLCBcbiAgICAgICAgICAgICAgICAgICAgICAgIHRpbWV2YXIgPSBcIlRlc3RcIixcbiAgICAgICAgICAgICAgICAgICAgICAgIGlkdmFyID0gYyhcIlN1YmplY3RcIiwgXCJHZW5kZXJcIiksXG4gICAgICAgICAgICAgICAgICAgICAgICBkaXJlY3Rpb24gPSBcIndpZGVcIilcblxuIyBSZXR1cm4gYHdpZGVfcmVzaGFwZWBcbndpZGVfcmVzaGFwZSIsInNjdCI6InRlc3Rfb2JqZWN0KFwid2lkZV9yZXNoYXBlXCIsIGluY29ycmVjdF9tc2c9XCJEb24ndCBmb3JnZXQgdG8gdXNlIGByZXNoYXBlKClgIHRvIGJyaW5nIHRoZSBkYXRhIHRvIGEgd2lkZSBmb3JtYXQ/XCIsIHVuZGVmaW5lZF9tc2c9XCJEaWQgeW91IHVzZSBgcmVzaGFwZSgpYCB0byByZXNoYXBlIHRoZSBkYXRhIHRvIGEgd2lkZSBmb3JtYXQ/XCIpXG50ZXN0X291dHB1dF9jb250YWlucyhcIndpZGVfcmVzaGFwZVwiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSByZXR1cm4gYHdpZGVfcmVzaGFwZWAgY29ycmVjdGx5P1wiKVxudGVzdF9lcnJvcigpXG5zdWNjZXNzX21zZyhcIkdyZWF0IGpvYiFcIikifQ==

Note that if you want you can also rename or sort the results of these new long and wide data formats! You can find detailed instructions below.

#### Reshaping Data Frames With tidyr

This package allows you to “easily tidy data with the spread() and gather() functions” and that’s exactly what you’re going to do if you use this package to reshape your data!

If you want to convert from wide to long format, the principle stays similar to the one that of reshape(): you use the gather() function and you start specifying its arguments: 1. Your data set is the first argument to the gather() function 2. Then, you specify the name of the column in which you will combine the the values of Read, Write and Listen. In this case, you want to call it something like Test or Test.Type. 3. You enter the name of the column in which all the values of the Read, Write and Listen columns are listed. 4. You indicate which columns are supposed to be combined into one. In this case, that will be the columns from Read, to Listen.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlN1YmplY3QgPC0gYygxLDIpXG5HZW5kZXIgPC0gYyhcIk1cIiwgXCJGXCIpXG5SZWFkIDwtIGMoMTAsIDcpXG5Xcml0ZSA8LWMoOCwgNClcbkxpc3RlbiA8LSBjKDcsIDYpXG5vYnNlcnZhdGlvbnNfd2lkZSA8LSBkYXRhLmZyYW1lKFN1YmplY3QsIEdlbmRlciwgUmVhZCwgV3JpdGUsIExpc3RlbikiLCJzYW1wbGUiOiIjIEltcG9ydCBgdGlkeXJgXG5saWJyYXJ5KC4uLi4uKVxuXG4jIEdhdGhlciB0aGUgd2lkZSBkYXRhIHRvIGEgbG9uZyBmb3JtYXRcbmxvbmdfdGlkeXIgPC0gLi4uLi4uKG9ic2VydmF0aW9uc193aWRlLCBcbiAgICAgICAgICAgICAgICAgICAgIFRlc3QsIFxuICAgICAgICAgICAgICAgICAgICAgUmVzdWx0LCBcbiAgICAgICAgICAgICAgICAgICAgIFJlYWQ6TGlzdGVuKVxuXG4jIFJldHVybiBgbG9uZ190aWR5cmBcbi4uLi4uLi4uLi4iLCJzb2x1dGlvbiI6IiMgSW1wb3J0IGB0aWR5cmBcbmxpYnJhcnkodGlkeXIpXG5cbiMgR2F0aGVyIHRoZSB3aWRlIGRhdGEgdG8gYSBsb25nIGZvcm1hdFxubG9uZ190aWR5ciA8LSBnYXRoZXIob2JzZXJ2YXRpb25zX3dpZGUsIFxuICAgICAgICAgICAgICAgICAgICAgVGVzdCwgXG4gICAgICAgICAgICAgICAgICAgICBSZXN1bHQsIFxuICAgICAgICAgICAgICAgICAgICAgUmVhZDpMaXN0ZW4pXG5cbiMgUmV0dXJuIGBsb25nX3RpZHlyYFxubG9uZ190aWR5ciIsInNjdCI6InRlc3RfZnVuY3Rpb24oXCJsaWJyYXJ5XCIsIGFyZ3M9XCJwYWNrYWdlXCIsIG5vdF9jYWxsZWRfbXNnPVwiRGlkIHlvdSBpbXBvcnQgdGhlIGB0aWR5cmAgcGFja2FnZT9cIiwgaW5jb3JyZWN0X21zZz1cIkRpZCB5b3UgaW1wb3J0IHRoZSBgdGlkeXJgIHBhY2thZ2UgY29ycmVjdGx5P1wiKVxudGVzdF9vYmplY3QoXCJsb25nX3RpZHlyXCIsIGluY29ycmVjdF9tc2c9XCJEaWQgeW91IGFkZCBgZ2F0aGVyKClgP1wiKVxudGVzdF9vdXRwdXRfY29udGFpbnMoXCJsb25nX3RpZHlyXCIsIG1pc3NpbmdfbXNnPVwiRGlkIHlvdSByZXR1cm4gYGxvbmdfdGlkeXJgP1wiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSByZXR1cm4gYGxvbmdfdGlkeXJgP1wiKVxudGVzdF9lcnJvcigpXG5zdWNjZXNzX21zZyhcIldlbGwgZG9uZSEgWW91ciBkYXRhIGlzIG5vdyBpbiBhIGxvbmcgZm9ybWF0IVwiKSJ9

Note how this the last argument specifies the columns in the same way as you did to subset writers_df or to select your writers_df’s columns in which you wanted to perform mathematical operations.

You can also just specify the columns individually like this:

long_tidyr <- gather(observations_wide,
Test,
Result,
Write,
Listen)
long_tidyr

Tip: try changing the code in the DataCamp Light box above to test this out!

The opposite direction, from long to wide format, is very similar to the function above, but this time with the spread() function:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkodGlkeXIpXG5TdWJqZWN0IDwtIGMoMSwyLDEsMiwyLDEpXG5HZW5kZXIgPC0gYyhcIk1cIiwgXCJGXCIsIFwiTVwiLCBcIkZcIiwgXCJGXCIsXCJNXCIpXG5UZXN0IDwtIGMoXCJSZWFkXCIsIFwiV3JpdGVcIiwgXCJXcml0ZVwiLCBcIkxpc3RlblwiLCBcIlJlYWRcIiwgXCJMaXN0Z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==

Again, you take as the first argument your data set. Then, you specify the column that contains the new column names.

In this case, that is Test.

Lastly, you input the name of the column that contains the values that should be put into the new columns.

Tip: take a look at the “Data Wrangling With dplyr And tidyr Cheat Sheet” for a complete overview of the possibilities that these packages can offer you to wrangle your data!

#### Reshaping Data Frames With reshape2

This package allows you to “flexibly reshape data”. To go from a wide to a long data format, you use its melt() function.

This function is pretty easy, since it just takes your data set and the id.vars argument, which you may already know from the reshape() function. This argument allows you to specify which columns should be left alone by the function.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlN1YmplY3QgPC0gYygxLDIpXG5HZW5kZXIgPC0gYyhcIk1cIiwgXCJGXCIpXG5SZWFkIDwtIGMoMTAsIDcpXG5Xcml0ZSA8LWMoOCwgNClcbkxpc3RlbiA8LSBjKDcsIDYpXG5vYnNlcnZhdGlvbnNfd2lkZSA8LSBkYXRhLmZyYW1lKFN1YmplY3QsIEdlbmRlciwgUmVhZCwgV3JpdGUsIExpc3RlbikiLCJzYW1wbGUiOiIjIEltcG9ydCBgcmVzaGFwZTJgXG5saWJyYXJ5KHJlc2hhcGUyKVxuXG4jIE1lbHQgdGhlIHdpZGUgb2JzZXJ2YXRpb25zIHRvIGxvbmcgZm9ybWF0XG5sb25nX3Jlc2hhcGVkMiA8LSAuLi4uKG9ic2VydmF0aW9uc193aWRlLCBcbiAgICAgICAgICAgICAgICAgICAgICAgaWQudmFycz1jKFwiU3ViamVjdFwiLCBcIkdlbmRlclwiKSxcbiAgICAgICAgICAgICAgICAgICAgICAgbWVhc3VyZS52YXJzPWMoXCJSZWFkXCIsIFwiV3JpdGVcIiwgXCJMaXN0ZW5cIiksXG4gICAgICAgICAgICAgICAgICAgICAgIHZhcmlhYmxlLm5hbWU9XCJUZXN0XCIsXG4gICAgICAgICAgICAgICAgICAgICAgIHZhbHVlLm5hbWU9XCJSZXN1bHRcIilcblxuIyBSZXR1cm4gYGxvbmdfcmVzaGFwZWQyYFxuLi4uLi4uLi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3VjY2Vzc2Z1bGx5IVwiKVxudGVzdF9vdXRwdXRfY29udGFpbnMoXCJsb25nX3Jlc2hhcGVkMlwiLCB0aW1lcyA9IDEsIGluY29ycmVjdF9tc2c9XCJEaWQgeW91IHJldHVybiBgbG9uZ19yZXNoYXBlZDJgIGNvcnJlY3RseT9cIilcbnRlc3RfZXJyb3IoKVxuc3VjY2Vzc19tc2coXCJHcmVhdCBqb2IhXCIpIn0=

But, as you will have noted, there are a couple more arguments specified in the code chunk above:

• measure.vars is there to name the destination column that will combine the original columns. If you leave out this argument, the melt() function will use all other variables as the id.vars.
• variable.name specifies how you want to name that destination column. If you don’t specify this argument, you will have a column named “variable” in your result.
• value.name allows you to input the name of the column in which the values or test results will be stored. If you leave out this argument, this column will be named “measurement”.

You can also go from a long to a wide format with the reshape2 package with the dcast() function.

This is fairly easy: you first give in your data set, as always. Then, you combine the columns which you don’t want to be touched;

In this case, you want to keep Subject and Gender as they are. The column Test however, you want to split! So, that is the second part of your second argument, indicated by a ~. The last argument of this function is value.var, which holds the values of the different tests. You want to name this new column Result:

### How To Sort A Data Frame

Sorting by columns might seem tricky, but this can be made easy by either using R’s built-in order() function or by using a package.

#### R’s Built-In Order() Function

You can for example sort by one of the dataframe’s columns. You order the rows according to the values that are stored in the variable Age.As.Writer:

writers_df[order(Age.As.Writer),]

If you want to sort the values starting from high to low, you can just add the extra argument decreasing, which can only take logical values.

Remember that logical values are TRUE or FALSE, respectively.

Another way is to add the function rev() so that it includes the order() function. As the function’s name suggests, it provides a way to give you the reversed version of its argument, which is order(Name) in this case:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoQWdlLkF0LkRlYXRoLCBBZ2UuQXMuV3JpdGVyLCBOYW1lLCBTdXJuYW1lLCBHZW5kZXIsIERlYXRoKSIsInNhbXBsZSI6IiMgVXNlIGBvcmRlcigpYCB0byBvcmRlclxud3JpdGVyc19kZltvcmRlcihOYW1lLCBkZWNyZWFzaW5nPVRSVUUpLF1cblxuIyBVc2UgYHJldigpYCB0byBvcmRlclxud3JpdGVyc19kZltyZXYob3JkZXIoTmFtZSkpLF1cblxuIyBPcmRlciBvbiBudW1lcmljIHZhcmlhYmxlXG53cml0ZXJzX2RmW29yZGVyKC1BZ2UuQXMuV3JpdGVyKSxdIn0=

You can also add a - in front of the numeric variable that you have given to order on.

Note that variables with other data types such as factors require you to convert them to characters or numeric before you can actually sort them:

as.character(Gender)

You can also sort on two variables. In that case, order() needs to have two arguments, so that you first sort by the first argument of the order() function and then on the second argument.

You’ll see an example of this further on in the tutorial.

#### Sorting With dplyr

The dplyr package, known for its abilities to manipulate data, has a specific function that allows you to sort rows by variables.

Dplyr’s function to make this happen is arrange().

The first argument of this function is the data set that you want to sort, while the second and third arguments are the variables that you choose to sort.

In this case you sort first on the variable Age.At.Death and then on Age.As.Writer:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuT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BpbmNvcnJlY3RfbXNnPVwiRG9uJ3QgZm9yZ2V0IHRvIGFkZCBgYXJyYW5nZSgpYCB0byB0aGUgY29kZSBzbyB0aGF0IHlvdSBjYW4gc29ydCB0aGUgZGF0YSBzdWNjZXNzZnVsbHkhXCIpXG50ZXN0X291dHB1dF9jb250YWlucyhcImRhdGEyXCIsIGluY29ycmVjdF9tc2c9XCJEaWQgeW91IHJldHVybiBgZGF0YTJgIGNvcnJlY3RseT9cIilcbnRlc3Rfb3V0cHV0X2NvbnRhaW5zKFwid3JpdGVyc19kZlt3aXRoKHdyaXRlcnNfZGYsIG9yZGVyKEFnZS5BdC5EZWF0aCwgQWdlLkFzLldyaXRlcikpLCBdXCIpXG50ZXN0X29iamVjdChcImRlc2Nfc29ydGVkX2RhdGFcIiwgdW5kZWZpbmVkX21zZz1cIkRvbid0IGZvcmdldCB0byBzb3J0IHRoZSBkYXRhIHdpdGggdGhlIGhlbHAgb2YgYGFycmFuZ2UoKWAhXCIsIGluY29ycmVjdF9tc2c9XCJEb24ndCBmb3JnZXQgdG8gYWRkIGBhcnJhbmdlKClgIHRvIHRoZSBjb2RlIHNvIHRoYXQgeW91IGNhbiBzb3J0IHRoZSBkYXRhIHN1Y2Nlc3NmdWxseSFcIilcbnRlc3Rfb3V0cHV0X2NvbnRhaW5zKFwiZGVzY19zb3J0ZWRfZGF0YVwiLCB0aW1lcz0xLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSByZXR1cm4gYGRlc2Nfc29ydGVkX2RhdGFgIGNvcnJlY3RseT9cIilcbnRlc3RfZXJyb3IoKVxuc3VjY2Vzc19tc2coXCJHcmVhdCBqb2IhXCIpIn0=

You can also use an approach where you use the with() function to get the same result.

Note that if you want to sort columns in descending order, you can add the function desc() to the variables.

Are you interested in doing much more with the dplyr package? Check out our Data Manipulation in R with dplyr course, which will teach you how to to perform sophisticated data manipulation tasks using dplyr!

Also, don’t forget to look at RStudio’s “Data Wrangling With dplyr And tidyr Cheat Sheet”!

#### Other Options To Sort Data Frames

There are also many other packages that offer sorting functions. This section will only give a short overview of the packages that exist. Firstly, the taRifx package offers sort.data.frame(), by which the values in Age.At.Death can again be sorted in decreasing order:

library(taRifx)
sorted_data <- sort(writers_df, decreasing=TRUE, ~Age.At.Death)
sorted_data

Thirdly, there’s also the package doBy that offers the function orderBy(). In this case, you want to order the values of Age.At.Death from high to low first, and then on the values of the Age.As.Writer variable.

library(doBy)
sorted_data_two <- orderBy(~-Age.At.Death+Age.As.Writer, writers_df)
sorted_data_two

### How To Merge Data Frames

#### Merging Data Frames On Column Names

You can use the merge() function to join two, but only two, data frames.

Let’s say you have data2, which has the same values stored in a variable Age.At.Death, which you also find in writers_df, with exactly the same values. You thus want to merge the two on the basis of this variable:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoQWdlLkF0LkRlYXRoLCBBZ2UuQXMuV3JpdGVyLCBOYW1lLCBTdXJuYW1lLCBHZW5kZXIsIERlYXRoKVxuZGF0YTIgPC0gZGF0YS5mcmFtZShBZ2UuQXQuRGVhdGg9YygyMiw0MCw3Miw0MSksIExvY2F0aW9uPTU6OCkiLCJzYW1wbGUiOiIjIE1lcmdlIGB3cml0ZXJzX2RmYCBhbmQgYGRhdGEyYFxubmV3X3dyaXRlcnNfZGYgPC0gLi4uLi4od3JpdGVyc19kZiwgZGF0YTIpXG5cbiMgUmV0dXJuIGBuZXdfd3JpdGVyc19kZmBcbi4uLi4uLi4uLi4uLi4uIiwic29sdXRpb24iOiIjIE1lcmdlIGB3cml0ZXJzX2RmYCBhbmQgYGRhdGEyYFxubmV3X3dyaXRlcnNfZGYgPC0gbWVyZ2Uod3JpdGVyc19kZiwgZGF0YTIpXG5cbiMgUmV0dXJuIGBuZXdfd3JpdGVyc19kZmBcbm5ld193cml0ZXJzX2RmIiwic2N0IjoidGVzdF9vYmplY3QoXCJuZXdfd3JpdGVyc19kZlwiLCBpbmNvcnJlY3RfbXNnPVwiRG9uJ3QgZm9yZ2V0IHRvIHVzZSBgbWVyZ2UoKWAgdG8gbWVyZ2UgYHdyaXRlcnNfZGZgIGFuZCBgZGF0YTJgIVwiLCB1bmRlZmluZWRfbXNnPVwiRGlkIHlvdSB1c2UgYG1lcmdlKClgIHRvIG1lcmdlIGB3cml0ZXJzX2RmYCBhbmQgYGRhdGEyYD8gXCIpXG50ZXN0X291dHB1dF9jb250YWlucyhcIm5ld193cml0ZXJzX2RmXCIsIGluY29ycmVjdF9tc2c9XCJEaWQgeW91IHJldHVybiBgbmV3X3dyaXRlcnNfZGZgIGNvcnJlY3RseT9cIilcbnRlc3RfZXJyb3IoKVxuc3VjY2Vzc19tc2coXCJXZWxsIGRvbmUhIFlvdSBoYXZlIHN1Y2Nlc3NmdWxseSBtZXJnZWQgZGF0YSBmcmFtZXMgb24gY29sdW1uIG5hbWVzIVwiKSJ9

You can easily merge the above two dataframes.

Tip: check what happens if you change the order of the two arguments of the merge() function!

This way of merging is equivalent to an outer join in SQL.

Unfortunately, you’re not always this lucky. In many cases, some of the columns names or variable values will differ, which makes it hard to follow the easy, standard procedure that was described just now. In addition, you may not always want to merge in the standard way that was described above.

In the following, some of the most common issues are listed and solved!

#### What If… (Some Of) The Data Frame’s Column Values Are Different?

If (some of) the values of the variable on which you merge differ, you have a small problem, because the merge() function supposes that these are the same so that any new variables that are present in the second data frame can be added correctly to the first.

Consider the following example:

> data2
x.Age.At.Death Location
1             21        5
2             39        6
3             71        7
4             40        8

You see that the values for the attribute Age.At.Death do not fit with the ones that were defined for writers_df.

No worries, the merge() function provides extra arguments to solve this problem.

The argument all.x allows you to specify to add the extra rows of the Location variable to the resulting data frame, even though this column is not present in writers_df.

In this case, the values of the Location variable will be added to writers_df for those rows of which the values of the Age.At.Death attribute correspond. All rows where the Age.At.Death of the two data frames don’t correspond, will be filled up with NA values.

Note that this join corresponds to a left outer join in SQL and that the default value of the all.x argument is FALSE, which means that one normally only takes into account the corresponding values of the merging variable.

Note also that you can also specify the argument all.y=TRUE if you want to add extra rows for each row that data2 has no matching row in writers_df.

For those who are familiar with SQL, this type of join correponds to a right outer join.

#### What If… Both Data Frames Have The Same Column Names?

What if your two data frames have exactly the same two variables, with or without the same values?

You can chose to keep all values from all corresponding variables and to add rows to the resulting data frame:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoQWdlLkF0LkRlYXRoLCBBZ2UuQXMuV3JpdGVyLCBOYW1lLCBTdXJuYW1lLCBHZW5kZXIsIERlYXRoKVxuZGF0YTIgPC0gZGF0YS5mcmFtZShBZ2UuQXQuRGVhdGg9YygyMSwzOSw3MSw0MCksIEFnZS5Bcy5Xcml0ZXI9YygxMSwyNSwzNiwyOCkpIiwic2FtcGxlIjoiIyBBZGQgcm93cyB0byB0aGUgcmVzdWx0XG5tZXJnZSh3cml0ZXJzX2RmLCBkYXRhMiwgYWxsPVRSVUUpXG5cbiMgQWRkIHZhbHVlcyB3aGVyZSBgQWdlLkF0LkRlYXRoYCBjb3JyZXNwb25kc1xubWVyZ2Uod3JpdGVyc19kZiwgZGF0YTIsIGJ5PVwiQWdlLkF0LkRlYXRoXCIpIn0=

Or you can just chose to add values from one specific variable for which the ages of death correspond.

#### What If… The Data Frames’ Column Names Are Different?

Lastly, what if the variable’s names on which you merge differ?

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IkFnZS5BdC5EZWF0aCA8LSBjKDIyLDQwLDcyLDQxKVxuQWdlLkFzLldyaXRlciA8LSBjKDE2LCAxOCwgMzYsIDM2KVxuTmFtZSA8LSBJKGMoXCJKb2huXCIsIFwiRWRnYXJcIiwgXCJXYWx0XCIsIFwiSmFuZVwiKSlcblN1cm5hbWUgPC0gSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwiLCBcIkF1c3RlblwiKSlcbkdlbmRlciA8LSBjKFwiTUFMRVwiLCBcIk1BTEVcIiwgXCJNQUxFXCIsIFwiRkVNQUxFXCIpXG5EZWF0aCA8LSBhcy5EYXRlKGMoXCIyMDE1LTA1LTEwXCIsIFwiMTg0OS0xMC0wN1wiLCBcIjE4OTItMDMtMjZcIixcIjE4MTctMDctMThcIikpXG53cml0ZXJzX2RmIDwtIGRhdGEuZnJhbWUoQWdlLkF0LkRlYXRoLCBBZ2UuQXMuV3JpdGVyLCBOYW1lLCBTdXJuYW1lLCBHZW5kZXIsIERlYXRoKVxuQWRkcmVzcyA8LSBjKFwiNTAgV2VzdCAxMHRoXCIsIFwiNzcgU3QuIE1hcmtzIFBsYWNlXCIsIFwiNzc4IFBhcmsgQXZlbnVlXCIpXG5NYXJpZWQgPC0gYyhcIllFU1wiLCBcIk5PXCIsIFwiWUVTXCIpXG5saW1pdGVkX3dyaXRlcnNfZGYgPC0gZGF0YS5mcmFtZShBZGRyZXNzLCBNYXJpZWQpIiwic2FtcGxlIjoiIyBNZXJnZSBgd3JpdGVyc19kZmAgYW5kIGBsaW1pdGVkX3dyaXRlcnNfZGZgXG53cml0ZXJzX3Jvd19zb3J0ZWQgPC0gbWVyZ2Uod3JpdGVyc19kZiwgbGltaXRlZF93cml0ZXJzX2RmLCBieT0wLCBhbGw9VFJVRSlcblxuIyBSZXR1biBgd3JpdGVyc19yb3dfc29ydGVkYFxud3JpdGVyc19yb3dfc29ydGVkIn0=

You just specify in the merge() function that there are two other specifications through the arguments by.x and by.y.

#### Merging Data Frames On Row Names

You can indeed merge the columns of two data frames, that contain a distinct set of columns but some rows with the same names. The merge() function and its arguments come to the rescue!

Consider this second example:

Address <- c("50 West 10th", "77 St. Marks Place", "778 Park Avenue")
Maried <- c("YES", "NO", "YES")
limited_writers_df <- data.frame(Address, Maried)

You see that this data set contains three rows, marked with numbers 1 to 3, and two additional columns that are not in writers_df. To merge these two, you add the argument by to the merge() function and set it at the number 0, which specifies the row names.

Since you choose to keep all values from all corresponding variables and to add columns to the result, you set the all argument to TRUE:

It could be that the fields for rows that don’t occur in both data structures result in NA-values. You can easily solve this by removing them.

How to do this will be discussed below.

### How To Remove Data Frame Rows And Columns With NA-Values

To remove all rows that contain NA-values, one of the easiest options is to use the na.omit() function, which takes your data frame as an argument.

Let’s recycle the code from the previous section, where you had a lot of resulting NA-values:

Note that the example above also demonstrates that if you just want to select part of the data frame from which you want to remove the NA-values, it’s better to use complete.cases().

In this case, you’re interested to keep all rows for which the values of the columns Age.As.Writer and Name are complete.

### How To Convert Between Data Structures

#### Convert Lists or Matrices To Data Frames

Lists or matrices that comply with the restrictions that the data frame structure imposes can be coerced into data frames with the as.data.frame() function.

Remember that a data frame is similar to the structure of a matrix, where the columns can be of different types. There are also similarities with lists, where each column is an element of the list and each element has the same length. Any matrices or lists that you want to convert need to satisfy with these restrictions.

For example, the matrix A can be converted because each column contains values of the numeric data type. You enter the matrix A as an argument to the as.data.frame() function:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJBID0gbWF0cml4KGMoMiwgNCwgMywgMSwgNSwgNyksIG5yb3c9MiwgbmNvbD0zLCBieXJvdyA9IFRSVUUpIFxuQV9kZiA8LSBhcy5kYXRhLmZyYW1lKEEpXG5BIn0=

You can follow the same procedures for lists like the one that is shown below:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJuID0gYygyLCAzLCA1KSBcbnMgPSBjKFwiYWFcIiwgXCJiYlwiLCBcImNjXCIpXG5iID0gYyhUUlVFLCBGQUxTRSwgVFJVRSlcbnggPSBsaXN0KG4sIHMsIGIsIDMpXG54X2RmIDwtIGFzLmRhdGEuZnJhbWUoeClcbnhfZGYifQ==

#### Convert Data Frames To Matrices Or Lists

To make the opposite move, that is, to convert data frames to matrices and lists, you first have to check for yourself if this is possible. Does your writers_df contain one or more dimensions and what about the amount of data types?

Rewatch the small animation of the introduction if you’re not sure what data structure to pick.

Once you have an answer, you can use the functions as.matrix() and as.list() to convert writers_df to a matrix or a list, respectively:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IndyaXRlcnNfZGYgPC0gZGF0YS5mcmFtZShjKDIyLDQwLDcyLDQxKSwgYygxNiwgMTgsIDM2LCAzNiksIEkoYyhcIkpvaG5cIiwgXCJFZGdhclwiLCBcIldhbHRcIiwgXCJKYW5lXCIpKSwgSShjKFwiRG9lXCIsIFw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GluY29ycmVjdF9tc2c9XCJEb24ndCBmb3JnZXQgdG8gYWRkIGBhcy5tYXRyaXgoKWAgdG8gbWFrZSBhIG1hdHJpeCBvdXQgb2YgYHdyaXRlcnNfZGZgIVwiKVxudGVzdF9vdXRwdXRfY29udGFpbnMoXCJ3cml0ZXJzX21hdHJpeFwiLCB0aW1lcz0xLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSByZXR1cm4gYHdyaXRlcnNfbWF0cml4YCBjb3JyZWN0bHk/XCIpXG50ZXN0X29iamVjdChcIndyaXRlcnNfbGlzdFwiLCB1bmRlZmluZWRfbXNnPVwiRGlkIHlvdSBhZGQgYGFzLmxpc3QoKWAgdG8gdGhlIGNvZGUgY2h1bmsgYWJvdmUgdG8gbWFrZSBhIGxpc3Qgb3V0IG9mIGB3cml0ZXJzX2RmYD9cIiwgaW5jb3JyZWN0X21zZz1cIkRpZCB5b3UgcmV0dXJuIGB3cml0ZXJzX2xpc3RgIGNvcnJlY3RseT9cIilcbnRlc3Rfb3V0cHV0X2NvbnRhaW5zKFwid3JpdGVyc19saXN0XCIsIHRpbWVzPTEsIGluY29ycmVjdF9tc2c9XCJEaWQgeW91IHJldHVybiBgd3JpdGVyc19saXN0YCBjb3JyZWN0bHk/XCIpXG50ZXN0X2Vycm9yKClcbnN1Y2Nlc3NfbXNnKFwiQW1hemluZyEgVGhhdCB3YXNuJ3QgdG9vIGhhcmQsIHdhcyBpdD9cIikifQ==

For those of you who want to specifically make numeric matrices, you can use the function data.matrix() or add an sapply() function to the as.matrix() function:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IndyaXRlcnNfZGYgPC0gZGF0YS5mcmFtZShjKDIyLDQwLDcyLDQxKSwgYygxNiwgMTgsIDM2LCAzNiksIEkoYyhcIkpvaG5cIiwgXCJFZGdhclwiLCBcIldhbHRcIiwgXCJKYW5lXCIpKSwgSShjKFwiRG9lXCIsIFwiUG9lXCIsIFwiV2hpdG1hblwi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WQgdGhlIGRhdGEgZnJhbWUgdHV0b3JpYWwgc3VjY2Vzc2Z1bGx5IVwiKSJ9

Note that with the current writers_df, which contains a mixture of data types, NA-values will be introduced in the resulting matrices.

## From Data Structures To Data Analysis, Data Manipulation and Data Visualization

Working with this R data structure is just the beginning of your data analysis!

If this tutorial has gotten you thrilled to dig deeper into programming with R, make sure to check out our free interactive Introduction to R course.

Those of you who are already more advanced with R and that want to take their skills to a higher level might be interested in our courses on data manipulation and data visualization.

Go to our course overview and take a look!

Related posts
python
+3