{"id":1477,"date":"2016-09-13T17:24:03","date_gmt":"2016-09-14T00:24:03","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?page_id=1477"},"modified":"2016-09-13T17:25:53","modified_gmt":"2016-09-14T00:25:53","slug":"combining-data-sets-in-r","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/summarizing-data\/combining-data-sets-in-r\/","title":{"rendered":"Combining data sets in R"},"content":{"rendered":"<p>It often happens that you want to combine information from one data frame with information from another data frame. This can look a few different ways.<\/p>\n<p>1. Adding rows<\/p>\n<p>If the two data frames contain exactly the same variables, you can use the command rbind (row-bind).<\/p>\n<p><code>result&lt;-rbind(df1, df2)<\/code><\/p>\n<p>Note that the variables don&#8217;t have to be in the same order!<\/p>\n<p>2. Adding columns<\/p>\n<p>If the two data frames contain exactly the same number of rows, and if you&#8217;re sure that the information will end up being appropriately matched row-by-row (for example if the second data frame contains only constants), you can use the cbind (column-bind)\u00a0command.<\/p>\n<p><code>result&lt;-cbind(df1, df2)<\/code><\/p>\n<p>However, usually you need some way of making sure that the new columns are matched with the correct rows. Consider trying to combine the following tables:<\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand1-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1481\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand1-1.png\" alt=\"rand1\" width=\"131\" height=\"120\" \/><\/a> <a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand2-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1482\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand2-1.png\" alt=\"rand2\" width=\"131\" height=\"121\" \/><\/a><\/p>\n<p>(Note that the numbers were generated using rnorm, one of R&#8217;s random number generating commands!) There are the same number of rows, but if you tried to use cbind you&#8217;d end up with Shirley&#8217;s information combined with Deanna&#8217;s. It would be possible to sort so that the tables were organized by alphabetical order of student name, but it&#8217;s much easier and safer to use the merge command. The basic command is simple:<\/p>\n<p><code>result&lt;-merge(df1, df2, by=\"student\")<\/code><\/p>\n<p>but in this case you could actually omit the &#8220;by&#8221; instruction: by default, merge looks to see whether there are any columns in the two data frames with the same name and uses that column (or those columns!) as the key.<\/p>\n<p>The result of this command is<\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1484\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand4.png\" alt=\"rand4\" width=\"185\" height=\"121\" \/><\/a><\/p>\n<p>This approach is smart if you have a 1-to-1 matching of information in your two tables, but it&#8217;s essential if the matching isn&#8217;t 1-to-1. What if you wanted to merge the first table above with the following?<\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand3-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1483\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand3-1.png\" alt=\"rand3\" width=\"130\" height=\"259\" \/><\/a><\/p>\n<p>Using the same merge command, <code>result&lt;-merge(df1, df2)<\/code>,\u00a0you arrive at the following:<\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1485\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand5.png\" alt=\"rand5\" width=\"185\" height=\"257\" \/><\/a><\/p>\n<p>You see here that what merge does by default\u00a0is form all valid combinations of the information from the two tables, where the\u00a0ID variable matches. So because Barend appeared 4 times in the table with var3, he appears 4 times in this table, and the same value for var1 is copied each time; because Gurpal doesn&#8217;t appear in the table with var3, he doesn&#8217;t appear in this result table. You can control this behaviour by setting the option &#8220;all&#8221;: you can choose to keep all values from just one of the tables, or from both. Let&#8217;s try keeping both. Here&#8217;s the command:<\/p>\n<p><code>result&lt;-merge(df1, df2, all=TRUE)<\/code><\/p>\n<p>and here&#8217;s the result:<\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1486\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2016\/09\/rand6.png\" alt=\"rand6\" width=\"184\" height=\"281\" \/><\/a><\/p>\n<p>Because Gurpal has no value for var3, his value for var1 is reported and an NA is given for var3.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It often happens that you want to combine information from one data frame with information from another data frame. This can look a few different ways. 1. Adding rows If the two data frames contain exactly the same variables, you can use the command rbind (row-bind). result&lt;-rbind(df1, df2) Note that the variables don&#8217;t have to [&hellip;]<\/p>\n","protected":false},"author":22979,"featured_media":0,"parent":548,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1477","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/1477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/users\/22979"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/comments?post=1477"}],"version-history":[{"count":2,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/1477\/revisions"}],"predecessor-version":[{"id":1488,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/1477\/revisions\/1488"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/548"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/media?parent=1477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}