{"id":858,"date":"2019-01-25T23:14:16","date_gmt":"2019-01-25T14:14:16","guid":{"rendered":"http:\/\/141.164.34.82\/?p=858"},"modified":"2022-02-03T17:30:08","modified_gmt":"2022-02-03T08:30:08","slug":"gather-spread","status":"publish","type":"post","link":"http:\/\/ds.sumeun.org\/?p=858","title":{"rendered":"\uc138\ub85c\ud615, \uac00\ub85c\ud615 \ubcc0\ud658(gather\/spread)"},"content":{"rendered":"<h1>\uc138\ub85c\ud615\/\uac00\ub85c\ud615 \ubcc0\ud658<\/h1>\n<ul>\n<li>\uc601\uc5b4\ub85c\ub294 \ubcf4\ud1b5 long-form\/wide-form\uc73c\ub85c \ubd88\ub9ac\ub294 \ub450 \ud615\ud0dc\ub97c \uc5ec\uae30\uc11c\ub294 <strong>\uc138\ub85c\ud615<\/strong>\/<strong>\uac00\ub85c\ud615<\/strong>\uc774\ub77c\uace0 \ubd80\ub974\uaca0\ub2e4.[<sup>g1]<\/sup><\/li>\n<\/ul>\n<p>[<sup>g1]:<\/sup> \ud55c \uc218\uac15\uc0dd\uc758 \uc81c\uc548\uc778\ub370 \uad1c\ucc2e\uc740 \uac83 \uac19\ub2e4.<\/p>\n<ul>\n<li>\uac00\ub85c\ud615(wide-form)\uc740 \ubcf4\ud1b5 \ud55c \uac1c\uccb4\uc5d0 \ub300\ud55c \uce21\uc815\uc744 \uc5ec\ub7ec \ubc88 \ud558\ub294 \ubc18\ubcf5 \uce21\uc815\uc5d0\uc11c \ub9ce\uc774 \uc4f0\uc778\ub2e4.<\/li>\n<li>\ub2e4\uc74c\uc740 \uac00\ub85c\ud615\uacfc \uc138\ub85c\ud615\uc744 \ubcc0\ud658\ud558\ub294 \uc608\uc81c\uc774\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">library(dplyr)\r\nlibrary(tidyr)\r\nmtcars$name = rownames(mtcars); rownames(mtcars) = NULL\r\nmtcars %&gt;% select(name, mpg, cyl, disp) -&gt; mtcars01\r\nhead(mtcars01)\r\n<\/code><\/pre>\n<pre>##                name  mpg cyl disp\r\n## 1         Mazda RX4 21.0   6  160\r\n## 2     Mazda RX4 Wag 21.0   6  160\r\n## 3        Datsun 710 22.8   4  108\r\n## 4    Hornet 4 Drive 21.4   6  258\r\n## 5 Hornet Sportabout 18.7   8  360\r\n## 6           Valiant 18.1   6  225\r\n<\/pre>\n<pre><code class=\"r\">mtcars01 %&gt;% gather(key='key', value='value', mpg, cyl, disp) -&gt; mtcarsLong\r\nhead(mtcarsLong)\r\n<\/code><\/pre>\n<pre>##                name key value\r\n## 1         Mazda RX4 mpg  21.0\r\n## 2     Mazda RX4 Wag mpg  21.0\r\n## 3        Datsun 710 mpg  22.8\r\n## 4    Hornet 4 Drive mpg  21.4\r\n## 5 Hornet Sportabout mpg  18.7\r\n## 6           Valiant mpg  18.1\r\n<\/pre>\n<pre><code class=\"r\">mtcarsLong %&gt;% spread(key='key', value='value') -&gt; mtcars02\r\nhead(mtcars02)\r\n<\/code><\/pre>\n<pre>##                 name cyl disp  mpg\r\n## 1        AMC Javelin   8  304 15.2\r\n## 2 Cadillac Fleetwood   8  472 10.4\r\n## 3         Camaro Z28   8  350 13.3\r\n## 4  Chrysler Imperial   8  440 14.7\r\n## 5         Datsun 710   4  108 22.8\r\n## 6   Dodge Challenger   8  318 15.5\r\n<\/pre>\n<pre><code class=\"r\">all.equal(mtcars01, mtcars02)\r\n<\/code><\/pre>\n<pre>## [1] \"Names: 3 string mismatches\"                      \r\n## [2] \"Component \\\"name\\\": 30 string mismatches\"        \r\n## [3] \"Component 2: Mean relative difference: 0.6920205\"\r\n## [4] \"Component 3: Mean relative difference: 36.28838\" \r\n## [5] \"Component 4: Mean relative difference: 0.9129228\"\r\n<\/pre>\n<pre><code class=\"r\">all.equal(mtcars01 %&gt;% arrange(name), mtcars02 %&gt;% select(name, mpg, cyl, disp) %&gt;% arrange(name))\r\n<\/code><\/pre>\n<pre>## [1] TRUE\r\n<\/pre>\n<ul>\n<li><code>gather<\/code>\ub294 \uc5ec\ub7ec \uceec\ub7fc\uc744 \ud558\ub098\uc758 \uceec\ub7fc\uc73c\ub85c \ubcc0\ud615\ud55c\ub2e4. <code>gather(key='key', value='value', ...)<\/code>\uc744 \uc77c\uc885\uc758 \uc5b4\uad6c(Idiom)\uc73c\ub85c \uc0dd\uac01\ud558\uace0, <code>...<\/code> \ubd80\ubd84\uc5d0 \ud558\ub098\uc758 \uceec\ub7fc\uc73c\ub85c \ubaa8\uc73c\uace0(gather) \uc2f6\uc740 \uceec\ub7fc\uc744 \uc801\uc5b4\ub098\uac00\uba74 \ub41c\ub2e4.\n<ul>\n<li><code>key<\/code>\uc640 <code>value<\/code>\ub294 \ubcc0\ud615\ub41c \ub370\uc774\ud130\uc758 \ub450 \uceec\ub7fc \uc774\ub984\uc774 \ub41c\ub2e4.<\/li>\n<\/ul>\n<\/li>\n<li><code>spread<\/code>\ub294 <code>gather<\/code>\uc758 \ubc18\ub300 \ubc29\ud5a5\uc73c\ub85c, \uc989 \uc138\ub85c\ud615 \ub370\uc774\ud130\ub97c \uac00\ub85c\ud615\uc73c\ub85c \ubcc0\ud615\ud558\uae30 \uc704\ud574 \uc0ac\uc6a9\ud55c\ub2e4.\n<ul>\n<li>\ub9cc\uc57d <code>gather(key='key', value='value', ...)<\/code>\ub85c \ub9cc\ub4e4\uc5b4\uc9c4 \uc790\ub8cc\ub77c\uba74 \uac04\ub2e8\ud558\uac8c <code>spread(key='key', value='value')<\/code>\uc744 \uc4f0\uba74 \ub41c\ub2e4.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>\ucc38\uace0\uc790\ub8cc<\/h3>\n<ul>\n<li><a href=\"https:\/\/www.datacamp.com\/community\/tutorials\/long-wide-data-R\">Long to Wide Data in R<\/a><\/li>\n<li><a href=\"https:\/\/basicstatistics.tistory.com\/entry\/long-formwide-formconceptual-understanding-and-R-implementation?category=507631\">long form\/wide form<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\uc138\ub85c\ud615\/\uac00\ub85c\ud615 \ubcc0\ud658 \uc601\uc5b4\ub85c\ub294 \ubcf4\ud1b5 long-form\/wide-form\uc73c\ub85c \ubd88\ub9ac\ub294 \ub450 \ud615\ud0dc\ub97c \uc5ec\uae30\uc11c\ub294 \uc138\ub85c\ud615\/\uac00\ub85c\ud615\uc774\ub77c\uace0 \ubd80\ub974\uaca0\ub2e4.[g1] [g1]: \ud55c \uc218\uac15\uc0dd\uc758 \uc81c\uc548\uc778\ub370 \uad1c\ucc2e\uc740 \uac83 \uac19\ub2e4. \uac00\ub85c\ud615(wide-form)\uc740 \ubcf4\ud1b5 \ud55c \uac1c\uccb4\uc5d0 \ub300\ud55c \uce21\uc815\uc744 \uc5ec\ub7ec \ubc88 \ud558\ub294 \ubc18\ubcf5 \uce21\uc815\uc5d0\uc11c \ub9ce\uc774 \uc4f0\uc778\ub2e4. \ub2e4\uc74c\uc740 \uac00\ub85c\ud615\uacfc \uc138\ub85c\ud615\uc744 \ubcc0\ud658\ud558\ub294 \uc608\uc81c\uc774\ub2e4. library(dplyr) library(tidyr) mtcars$name = rownames(mtcars); rownames(mtcars) = NULL mtcars %&gt;% select(name, mpg, cyl, disp) -&gt; mtcars01 head(mtcars01) ## name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2474,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28,114],"tags":[116,115,117,118,119],"jetpack_featured_media_url":"http:\/\/ds.sumeun.org\/wp-content\/uploads\/2019\/01\/wood-g8ed7269dc_640.jpg","_links":{"self":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/858"}],"collection":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=858"}],"version-history":[{"count":3,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/858\/revisions"}],"predecessor-version":[{"id":2475,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/858\/revisions\/2475"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/media\/2474"}],"wp:attachment":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=858"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}