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!

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:

Be careful, however, because it’s easy to make errors while doing this!

You can then start filling your ab up by using the [,] notation.

The easiest way to create an empty data frame is probably by just assigning a data.frame() function without any arguments to a vector:

You can use factor() to remove the factor levels that are no longer present, you can enter the following line of code.

Notice how all factor levels of this column still remain present, even though you have created a subset:

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 .

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”.

In a next step, you can try subsetting with the subset() function:

To make sure that this doesn’t happen, you can add the argument drop=FALSE :

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.

Tip: be careful when you are subsetting just one column!

Note that you can also define this subset with the variable names.

It’s easy to start subsetting with the [,] notation that was described in step two:

You basically extract those values from the rows and columns that you need in order to optimize the data analyses you make.

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.

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.

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.

If you want to remove values or entire columns, you can assign a NULL value to the desired unit:

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:

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

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 :

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 inst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2VydmF0aW9uc19sb25nIiwic2N0IjoidGVzdF9vYmplY3QoXCJTdWJqZWN0XCIpXG50ZXN0X29iamVjdChcIkdlbmRlclwiKVxudGVzdF9vYmplY3QoXCJUZXN0XCIpXG50ZXN0X29iamVjdChcIlJlc3VsdFwiKVxudGVzdF9vYmplY3QoXCJvYnNlcnZhdGlvbnNfbG9uZ1wiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSBtYWtlIGEgZGF0YSBmcmFtZSB3aXRoIGBkYXRhLmZyYW1lKClgP1wiLCB1bmRlZmluZWRfbXNnPVwiRGlkIHlvdSBtYWtlIGEgZGF0YSBmcmFtZSB3aXRoIGBkYXRhLmZyYW1lKClgP1wiKVxudGVzdF9vdXRwdXRfY29udGFpbnMoXCJvYnNlcnZhdGlvbnNfbG9uZ1wiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSByZXR1cm4gdGhlIGRhdGEgZnJhbWUgYG9ic2VydmF0aW9uc19sb25nYD9cIilcbnRlc3RfZXJyb3IoKVxuc3VjY2Vzc19tc2coXCJHb29kIGpvYiEgTm93IHN0dWR5IHdoYXQgZGF0YSBpbiBhIGxvbmcgZm9ybWF0IGxvb2tzIGxpa2UuXCIpIn0=

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: 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”. Next, add the argument v.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 . 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”. Then, you add the argument the argument times , 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”. You’re finally there! Give in the end format for the data with the argument direction . Additionally, you can specify new row names with the argument new.row.names .

Tip: try leaving out this last argument and see what happens! eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlN1YmplY3QgPC0gYygxLDIsMSwyLDIsMSlcbkdlbmRlciA8LSBjKFwiTVwiLCBcIkZcIiwgXCJNXCIsIFwiRlwiLCBcIkZcIixcIk1cIilcblRlc3QgPC0gYyhcIlJlYWRcIiwgXCJXcml0ZVwiLCBcIldyaXRlXCIsIFwiTGlzdGVuXCIsIFwiUmVhZFwiLCBcIkxpc3RlblwiKVxuUmVzdWx0IDwtIGMoMTAsIDQsIDgsIDYsIDcsIDcpXG5vYnNlcnZhdGlvbnNfbG9uZyA8LSBkYXRhLmZyYW1lKFN1YmplY3QsIEdlbmRlciwgVGVzdCwgUmVzdWx0KSIsInNhbXBsZSI6IiMgSW1wb3J0IGByZXNoYXBlMmBcbmxpYnJhcnkoLi4uLi4uLi4uKVxuXG4jIENvbnZlcnQgdG8gYSB3aWRlIGZvcm1hdCB3aXRoIGBkY2FzdCgpYFxubG9uZ19yZXNoYXBlZDIgPC0gLi4uLi4ob2JzZXJ2YXRpb25zX2xvbmcsIFxuICAgICAgICAgICAgICAgICAgICAgICAgU3ViamVjdCArIEdlbmRlciB+IFRlc3QsIFxuICAgICAgICAgICAgICAgICAgICAgICAgdmFsdWUudmFyPVwiUmVzdWx0XCIpXG5cbiMgUmV0dXJuIGBsb25nX3Jlc2hhcGVkMmBcbi4uLi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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: timevar allows you to specify that the variable Test , which describes the different tests that you give to your students, should be decomposed. 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 . By not naming the variable Result , the reshape() function will know that both Test and Result should be recombined. 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 . eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlN1YmplY3QgPC0gYygxLDIpX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3J0IHRoZSBgdGlkeXJgIHBhY2thZ2UgY29ycmVjdGx5P1wiKVxudGVzdF9vYmplY3QoXCJsb25nX3RpZHlyXCIsIGluY29ycmVjdF9tc2c9XCJEaWQgeW91IGFkZCBgZ2F0aGVyKClgP1wiKVxudGVzdF9vdXRwdXRfY29udGFpbnMoXCJsb25nX3RpZHlyXCIsIG1pc3NpbmdfbXNnPVwiRGlkIHlvdSByZXR1cm4gYGxvbmdfdGlkeXJgP1wiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSByZXR1cm4gYGxvbmdfdGlkeXJgP1wiKVxudGVzdF9lcnJvcigpXG5zdWNjZXNzX21zZyhcIldlbGwgZG9uZSEgWW91ciBkYXRhIGlzIG5vdyBpbiBhIGxvbmcgZm9ybWF0IVwiKSJ9



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, Read, 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: eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkodGlkeXIpXG5TdWJqZWN0IDwtIGMoMSwyLDEsMiwyLDEpXG5HZW5kZXIgPC0gYyhcIk1cIiwgXCJGXCIsIFwiTVwiLCBcIkZcIiwgXCJGXCIsXCJNXCIpXG5UZXN0IDwtIGMoXCJSZWFkXCIsIFwiV3JpdGVcIiwgXCJXcml0ZVwiLCBcIkxpc3RlblwiLCBcIlJlYWRcIiwgXCJMaXN0ZW5cIilcblJlc3VsdCA8LSBjKDEwLCA0LCA4LCA2LCA3LCA3KVxub2JzZXJ2YXRpb25zX2xvbmcgPC0gZGF0YS5mcmFtZShTdWJqZWN0LCBHZW5kZXIsIFRlc3QsIFJlc3VsdCkiLCJzYW1wbGUiOiIjIFNwcmVhZCB0aGUgbG9uZyBkYXRhIHRvIHdpZGUgZm9ybWF0IHdpdGggYHNwcmVhZCgpYFxud2lkZV90aWR5ciA8LSBzcHJlYWQob2JzZXJ2YXRpb25zX2xvbmcsIFxuICAgICAgICAgICAgICAgICAgICAgVGVzdCwgXG4gICAgICAgICAgICAgICAgICAgICBSZXN1bHQpXG5cbiMgUmV0dXJuIGB3aWRlX3RpZHlyYFxuLi4uLi4uLi4uLiIsInNvbHV0aW9uIjoiIyBTcHJlYWQgdGhlIGxvbmcgZGF0YSB0byB3aWRlIGZvcm1hdCB3aXRoIGBzcHJlYWQoKWBcbndpZGVfdGlkeXIgPC0gc3ByZWFkKG9ic2VydmF0aW9uc19sb25nLCBcbiAgICAgICAgICAgICAgICAgICAgIFRlc3QsIFxuICAgICAgICAgICAgICAgICAgICAgUmVzdWx0KVxuXG4jIFJldHVybiBgd2lkZV90aWR5cmBcbndpZGVfdGlkeXIiLCJzY3QiOiJ0ZXN0X29iamVjdChcIndpZGVfdGlkeXJcIiwgdW5kZWZpbmVkX21zZz1cIkRpZCB5b3UgZm9yZ2V0IHRvIGFkZCBgc3ByZWFkKClgIHRvIHNwcmVhZCB0aGUgbG9uZyBkYXRhIHRvIHdpZGUgZm9ybWF0P1wiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSBhZGQgYHNwcmVhZCgpYD9cIilcbnRlc3Rfb3V0cHV0X2NvbnRhaW5zKFwid2lkZV90aWR5clwiLCBpbmNvcnJlY3RfbXNnPVwiRGlkIHlvdSByZXR1cm4gYHdpZGVfdGlkeXJgP1wiKVxudGVzdF9lcnJvcigpXG5zdWNjZXNzX21zZyhcIldlbGwgZG9uZSFcIikifQ==



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!