Warning: Parameter 2 to wp_hide_post_Public::query_posts_join() expected to be a reference, value given in /home/rexercis/public_html/wp-includes/class-wp-hook.php on line 286R-exercises – Melt and Cast The Shape of Your Data-Frame: Exercises

Melt and Cast The Shape of Your Data-Frame: Exercises

Warning: Parameter 2 to wp_hide_post_Public::query_posts_join() expected to be a reference, value given in /home/rexercis/public_html/wp-includes/class-wp-hook.php on line 286

Difficulty level: Not rated yet

Data-sets often arrive to us in a form that is different from what we need for our modeling or visualization functions, which, in turn, don’t necessarily require the same format.

Reshaping data.frames is a step that all analysts need, but many struggle with. Practicing this meta-skill will, in the long-run, result in more time to focus on the actual analysis.

The solutions to this set will rely on data.table, mostly melt() and dcast(), which are originally from the reshape2 package. However, you can also get practice out of it using your favorite base-R, tidy-verse or any other method, then compare the results.

Revisiting a data-set used in an earlier exercise set on data exploration,
load the AER package and run the command data("Fertility"), which loads the data-set Fertility to your work space.
Melt it into the following format, with one row per child.

head(ferl)
morekids age afam hispanic other work mother_id order gender
1 no 27 no no no 0 1 1 male
2 no 30 no no no 30 2 1 female
3 no 27 no no no 0 3 1 male
4 no 35 yes no no 0 4 1 male
5 no 30 no no no 22 5 1 female
6 no 26 no no no 40 6 1 male

Ruse 28 February 2019 at 18:00 on Mode exercisesHow about this? # Form a vector y that has the single element '123' by using the paste() function (along...

Let’s Spread the Word about R-exercises!

If you enjoy our free exercises, we’d like to ask you a small favor: Please help us spread the word about R-exercises. Go to your preferred site with resources on R, either within your university, the R community, or at work, and kindly ask the webmaster to add a link to www.r-exercises.com. We very much appreciate your help!