{"id":948,"date":"2019-03-13T02:54:28","date_gmt":"2019-03-12T17:54:28","guid":{"rendered":"http:\/\/141.164.34.82\/?p=948"},"modified":"2019-03-13T15:56:42","modified_gmt":"2019-03-13T06:56:42","slug":"%ea%b0%80%eb%a1%9c%ed%98%95-%ec%84%b8%eb%a1%9c%ed%98%95-reshape2melt-dcast","status":"publish","type":"post","link":"http:\/\/ds.sumeun.org\/?p=948","title":{"rendered":"\uac00\ub85c\ud615\/\uc138\ub85c\ud615 \ubcc0\ud658(reshape2::melt\/dcast)"},"content":{"rendered":"<ul>\n<li>\ub370\uc774\ud130 \uc900\ube44<\/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, am, mpg, cyl, disp) -&gt; mcars\r\nhead(mcars, 4)\r\n<\/code><\/pre>\n<pre>##             name am  mpg cyl disp\r\n## 1      Mazda RX4  1 21.0   6  160\r\n## 2  Mazda RX4 Wag  1 21.0   6  160\r\n## 3     Datsun 710  1 22.8   4  108\r\n## 4 Hornet 4 Drive  0 21.4   6  258\r\n<\/pre>\n<h3>\ub370\uc774\ud130<\/h3>\n<pre><code class=\"r\">library(dplyr)\r\nlibrary(tidyr)\r\nmtcars$name = rownames(mtcars); rownames(mtcars) = NULL\r\nmtcars %&gt;% select(name, am, mpg, cyl, disp) -&gt; mcars\r\nhead(mcars)\r\n<\/code><\/pre>\n<pre>##   name am  mpg cyl disp\r\n## 1    1  1 21.0   6  160\r\n## 2    2  1 21.0   6  160\r\n## 3    3  1 22.8   4  108\r\n## 4    4  0 21.4   6  258\r\n## 5    5  0 18.7   8  360\r\n## 6    6  0 18.1   6  225\r\n<\/pre>\n<h1>\ud328\ud0a4\uc9c0 <code>reshape2<\/code>\uc758 \ud65c\uc6a9<\/h1>\n<p>\ud328\ud0a4\uc9c0 <code>reshape2<\/code>\uc758 <code>melt<\/code>\/<code>dcast<\/code> \ud568\uc218\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc138\ub85c\ud615\/\uac00\ub85c\ud615 \ubcc0\ud658\uc744 \ud560 \uc218\ub3c4 \uc788\ub2e4. \ub2e4\uc74c \ucf54\ub4dc\ub294 \uc55e\uc5d0\uc11c <a href=\"http:\/\/141.164.34.82\/?p=858\"><code>gather<\/code>\/<code>spread<\/code> \ud568\uc218\uac00 \ud55c \uc138\ub85c\ud615\/\uac00\ub85c\ud615 \ubcc0\ud658<\/a>\uc744 <code>melt<\/code>\/<code>dcast<\/code> \ud568\uc218\ub97c \uc368\uc11c \uc7ac\uc5f0\ud558\uace0 \uc788\ub2e4. \uacb0\uacfc\uac00 \uac19\ub2e4!<\/p>\n<pre><code class=\"r\">library(reshape2)\r\n<\/code><\/pre>\n<pre>## \r\n## Attaching package: 'reshape2'\r\n<\/pre>\n<pre>## The following object is masked from 'package:tidyr':\r\n## \r\n##     smiths\r\n<\/pre>\n<pre>## The following objects are masked from 'package:data.table':\r\n## \r\n##     dcast, melt\r\n<\/pre>\n<pre><code class=\"r\">mcarsLong &lt;- \r\n  mcars %&gt;% gather(-name, -am, \r\n                   mpg, cyl, disp, \r\n                   key='key', value='value', \r\n                   factor_key=TRUE) \r\nmcarsWide &lt;- \r\n  mcarsLong %&gt;% spread(key='key', value='value') \r\n\r\nmcarsLong2 &lt;- \r\n  mcars %&gt;% melt(id=c(\"name\",\"am\"),\r\n                 measure.vars = c(\"mpg\", \"cyl\", \"disp\"),\r\n                 variable.name = \"key\", value.name = 'value')\r\nmcarsWide2 &lt;- \r\n  mcarsLong2 %&gt;% dcast(name + am ~ key) -&gt; mcarsWide2\r\n\r\nall.equal(mcarsLong, mcarsLong2)\r\nall.equal(mcarsWide, mcarsWide2)\r\n<\/code><\/pre>\n<pre>## [1] TRUE\r\n## [1] TRUE\r\n<\/pre>\n<p>\ucc28\uadfc \ucc28\uadfc \ub450 \ucf54\ub4dc\ub97c \ube44\uad50\ud574\ubcf4\uc790. <code>gather<\/code>\uc758 \uacbd\uc6b0 \ub370\uc774\ud130 \ud14c\uc774\ube14\uc758 \uc5f4\uc5d0\uc11c \uba85\uc2dc\uc801\uc73c\ub85c \uc9c0\uc815\ub41c \uc5f4\uc740 <strong>\uce21\uc815\ubcc0\uc218<\/strong>\ub85c, \uadf8 \uc678\uc758 \ubcc0\uc218\ub294 <strong>\uc2dd\ubcc4\uc790<\/strong>(identifier)\ub85c \ud65c\uc6a9\ud55c\ub2e4. \uc2dd\ubcc4\uc790\ub97c \uba85\uc2dc\ud558\ub824\uba74 \ubcc0\uc218\uba85 \uc55e\uc5d0 <code>-<\/code>\ub97c \ubd99\uc774\uba74 \ub41c\ub2e4. \uc774\ub807\uac8c \ud558\uba74 \uc138\ub85c\ud615\uc73c\ub85c \ubcc0\ud658\ud558\ub824\ub294 \ub370\uc774\ud130\ud504\ub808\uc784\uc758 \ubaa8\ub4e0 \uc5f4\uc744 <strong>\uc2dd\ubcc4\uc790<\/strong>\uc640 <strong>\uce21\uc815\ubcc0\uc218<\/strong>\ub85c \uad6c\ubd84\ud558\uc5ec <code>gather<\/code> \ud568\uc218\uc5d0 \uc54c\ub824\uc904 \uc218 \uc788\ub2e4. (\ubb34\uc5c7\ubcf4\ub2e4 \uc6b0\ub9ac\uac00 <strong>\uc2dd\ubcc4\uc790<\/strong>\uc640 <strong>\uce21\uc815\ubcc0\uc218<\/strong>\ub97c \uba85\ud655\ud558\uac8c \uad6c\ubd84\ud560 \uc218 \uc788\ub2e4.) <code>key=<\/code>\uc640 <code>value=<\/code>\ub294 \ubcc0\ud658\ub41c \uc138\ub85c\ud615\uc758 \uc5f4\uc774\ub984\uc73c\ub85c \uc5ec\uae30\uc11c\ub294 \uae30\ubcf8\uac12\uc778 <code>key<\/code>\uc640 <code>value<\/code>\ub85c \ud558\uc600\ub2e4. \uadf8\ub9ac\uace0 <code>factor_key=TRUE<\/code>\ub85c \ud558\uc5ec <code>melt<\/code>\uc758 \uacb0\uacfc\uc640 \uc77c\uce58\ud558\uac8c \ub9cc\ub4e4\uc5c8\ub2e4. (<code>factor_key=TRUE<\/code>\ub294 <code>key<\/code> \uc5f4\uc744 \ud329\ud130\ud615\uc73c\ub85c \ub9cc\ub4e0\ub2e4.)<\/p>\n<p><code>gather<\/code>\ub97c <code>melt<\/code> \ud568\uc218\ub85c \uad6c\ud604\ud558\ub824\uba74 \uc5ed\uc2dc <strong>\uc2dd\ubcc4\uc790<\/strong>\uc640 <strong>\uce21\uc815\ubcc0\uc218<\/strong>\ub97c \uba85\uc2dc\ud55c\ub2e4. <code>id=<\/code>\uc5d0 <strong>\uc2dd\ubcc4\uc790<\/strong> \ubcc0\uc218\uc774\ub984\uc744 \ubb38\uc790\uc5f4 \ubca1\ud130\ub85c, <code>measure.vars=<\/code>\uc5d0 <strong>\uce21\uc815\ubcc0\uc218<\/strong> \uc774\ub984\uc744 \ubb38\uc790\uc5f4 \ubca1\ud130\ub85c \uc785\ub825\ud55c\ub2e4. \uadf8\ub9ac\uace0 <code>variable.name=<\/code>\uacfc <code>value.name=<\/code>\uc740 <code>gather<\/code>\uc758 <code>key=<\/code>\uc640 <code>value=<\/code>\uc5d0 \ub300\uc751\ud558\ub294 \uc778\uc790\uc774\ub2e4. \uc138\ub85c\ud615\uc758 \uc5f4 \uc774\ub984\uc744 \uacb0\uc815\ud55c\ub2e4. <code>dcast<\/code>\uc758 \uae30\ubcf8\uac12\uc740 <code>variable<\/code>\uacfc <code>value<\/code>\uc774\uc9c0\ub9cc <code>gather<\/code> \uacb0\uacfc\uc640 \uc77c\uce58\ud558\uac8c \ub9cc\ub4e4\uae30 \uc704\ud574 <code>key<\/code>\uc640 <code>value<\/code>\ub85c \ud558\uc600\ub2e4.<\/p>\n<p><code>spread(dat, key='key', value='value')<\/code>\ub294 \uc77c\uc885\uc758 \uc5b4\uad6c\ub85c \uc0dd\uac01\ud588\ub2e4. (\uc5f4\uc774\ub984\uc774 \ubc14\ub00c\uc5c8\ub2e4\uba74 \ubc18\uc601\ud574\uc11c <code>key=<\/code>\uacfc <code>value=<\/code>\ub97c \ubc14\uafbc\ub2e4.) \uc774\ub97c <code>dcast<\/code>\ub85c \uad6c\ud604\ud558\ub294 \ubc29\ubc95\uc744 \uc54c\uc544\ubcf4\uc790. \ud615\uc2dd\uc740 <code>dcast(dat, ~ )<\/code>\uaf34\uc774\uace0, <code>~<\/code>\uc758 \uc67c\ucabd\uc5d0 <strong>\uc2dd\ubcc4\uc790<\/strong> \ubcc0\uc218\ub97c, \uc624\ub978\ucabd\uc5d0 <code>key<\/code>\ub97c \uc801\ub294\ub2e4.<\/p>\n<p>\uc774\ub807\uac8c <code>melt<\/code>\/<code>dcast<\/code>\ub85c \uc138\ub85c\ud615\/\uac00\ub85c\ud615 \ubcc0\ud658\uc744 \ud560 \uc218 \uc788\uc74c\uc744 \ubcf4\uc600\ub2e4. \uadf8\ub7f0\ub370 \ub611\uac19\uc740 \uc77c\uc744 \uc65c \uad73\uc774 \ub2e4\ub978 \ud568\uc218\ub85c \ud558\ub294\uac74\uac00? <code>melt<\/code>\uc640 <code>dcast<\/code>\ub294 \uc138\ub85c\ud615\/\uac00\ub85c\ud615 \ubcc0\ud658 \ubfd0 \uc544\ub2c8\ub77c \uc9d1\uacc4\ud1b5\uacc4\uce58(\ub610\ub294 \uc694\uc57d \ud1b5\uacc4\uce58)\uc744 \uad6c\ud558\ub294 \ub370\uc5d0\ub3c4 \uc0ac\uc6a9\ud560 \uc218 \uc788\uae30 \ub54c\ubb38\uc774\ub2e4.<\/p>\n<p>\uc55e\uc758 \ucf54\ub4dc\uc5d0\uc11c <code>mcarsLong<\/code>\uacfc <code>mcarsLong2<\/code>\uc758 <strong>\uc2dd\ubcc4\uc790<\/strong>\ub294 <code>name<\/code>\uacfc <code>am<\/code>\uc774\uc5c8\ub2e4. \ub9cc\uc57d <code>am<\/code>\uc758 \uac12\uc5d0 \ub530\ub978 <strong>\uce21\uc815\ubcc0\uc218<\/strong> <code>mpg<\/code>, <code>cyl<\/code>, <code>disp<\/code>\uc758 \ud3c9\uade0\uc744 \uc54c\uace0 \uc2f6\ub2e4\uba74 \ub2e4\uc74c\uacfc \uac19\uc774 \ud55c\ub2e4.<\/p>\n<pre><code class=\"r\">dcast(mcarsLong2, am ~ key, fun.aggregate=mean)\r\n<\/code><\/pre>\n<pre>##   am      mpg      cyl     disp\r\n## 1  0 17.14737 6.947368 290.3789\r\n## 2  1 24.39231 5.076923 143.5308\r\n<\/pre>\n<p>\uc55e\uc758 \uac00\ub85c\ud615 \ubcc0\ud658(<code>dcast(mcarsLong2, name + am ~ key)<\/code>)\uacfc \ube44\uad50\ud574 \ubcf4\uba74 <code>~<\/code>\uc758 \uc88c\uce21\uc5d0 <code>name<\/code>\uc774 \ube60\uc9c4 \uac83\uc744 \ud655\uc778\ud560 \uc218 \uc788\ub2e4. \uadf8\ub9ac\uace0 \uacb0\uacfc\ub97c \ubcf4\uba74 \ud45c\uc758 \uc88c\uce21\uc5d0 <code>am<\/code>\uc758 \uac12\uc774 \ub098\uc5f4\ub418\uace0, \uc6b0\uce21\uc5d0 <strong>\uce21\uc815\ubcc0\uc218<\/strong>(<code>key<\/code>)\uac00 \ub098\uc5f4\ub418\uc5b4 \uc788\uc74c\uc744 \ubcfc \uc218 \uc788\ub2e4.<\/p>\n<p>\ub2e4\uc2dc <code>dcast(mcarsLong2, name + am ~ key)<\/code> \ub610\ub294 \uc6d0\uc790\ub8cc\ub97c \uc0b4\ud3b4\ubcf4\uc790.<\/p>\n<pre><code class=\"r\">dcast(mcarsLong2, name + am ~ key) %&gt;% head(5)\r\n<\/code><\/pre>\n<pre>##   name am  mpg cyl  disp\r\n## 1    1  1 21.0   6 160.0\r\n## 2   10  0 19.2   6 167.6\r\n## 3   11  0 17.8   6 167.6\r\n## 4   12  0 16.4   8 275.8\r\n## 5   13  0 17.3   8 275.8\r\n<\/pre>\n<p>\uc774 \ub370\uc774\ud130\ud504\ub808\uc784 \uccab \ud589\uc744 \ubcf4\uc790. \uc774\ub984(<code>name<\/code>)\uc774 <code>AMC Javelin<\/code>\uc774\uace0 <code>am==0<\/code>(\uc790\ub3d9\uae30\uc5b4)\uc778 \ucc28\uc758 3\uac00\uc9c0 \uce21\uc815\ubcc0\uc218 <code>mpg<\/code>, <code>cyl<\/code>, <code>disp<\/code>\uc744 \ubcf4\uc5ec\uc900\ub2e4. \ub9cc\uc57d <strong>\uc2dd\ubcc4\uc790<\/strong>\ub85c \uc4f0\uc778 <code>name<\/code>\uacfc <code>am<\/code>\uc5d0\uc11c <code>name<\/code>\uc744 \uc0dd\ub7b5\ud558\uace0 <code>am<\/code>\ub9cc \uc0ac\uc6a9\ud55c\ub2e4\uba74 <code>am==0<\/code>\uc778 \uc5ec\ub7ec \uc790\ub3d9\ucc28\uc758 \ub370\uc774\ud130\uac00 \ud55c \ud589\uc5d0 \ubaa8\uc774\uac8c \ub41c\ub2e4. \uc774\ub4e4\uc744 \ud558\ub098\uc758 \uac12\uc73c\ub85c \ubc14\uafb8\uae30 \uc704\ud574\uc11c <code>fun.aggregate=<\/code>\uc758 \ud568\uc218\ub97c \uc801\uc6a9\ud55c \uacb0\uacfc\uac00 <code>dcast(mcarsLong2, am ~ key, fun.aggregate=mean)<\/code>\uc774\ub2e4.<\/p>\n<p>\uc774\uac83\uc740 \uc55e\uc5d0\uc11c \ubd24\ub358 \ub2e4\ucc28\uc6d0 \ud589\ub82c\uc758 \ud55c \ucc28\uc6d0\uc744 \uc81c\uac70\ud558\uc5ec \ucc28\uc6d0\uc744 \ucd95\uc18c\ud558\uba74\uc11c \uc9d1\uacc4 \ud568\uc218\ub97c \uc801\uc6a9\ud558\ub294 \uac83\uacfc \ube44\uc2b7\ud558\ub2e4. <code>dcast<\/code>\ub294 \ucc28\uc6d0\uc744 \ucd95\uc18c\ud558\uba74\uc11c \uc6d0\ud558\ub294 \ud615\ud0dc\uc758 \ud45c\ub85c \uc790\ub8cc\ub97c \ubcc0\ud658\ud560 \uc218 \uc788\ub2e4. \ub2e4\uc74c\uc758 \ucf54\ub4dc\ub97c \uc2e4\ud589\ud574\ubcf4\uc790.<\/p>\n<pre><code class=\"r\">dcast(mcarsLong2, key ~ am, fun.aggregate=mean)\r\n<\/code><\/pre>\n<pre>##    key          0          1\r\n## 1  mpg  17.147368  24.392308\r\n## 2  cyl   6.947368   5.076923\r\n## 3 disp 290.378947 143.530769\r\n<\/pre>\n<pre><code class=\"r\">dcast(mcarsLong2, . ~ am + key, fun.aggregate=mean)\r\n<\/code><\/pre>\n<pre>##   .    0_mpg    0_cyl   0_disp    1_mpg    1_cyl   1_disp\r\n## 1 . 17.14737 6.947368 290.3789 24.39231 5.076923 143.5308\r\n<\/pre>\n<pre><code class=\"r\">dcast(mcarsLong2, . ~ key + am, fun.aggregate=mean)\r\n<\/code><\/pre>\n<pre>##   .    mpg_0    mpg_1    cyl_0    cyl_1   disp_0   disp_1\r\n## 1 . 17.14737 24.39231 6.947368 5.076923 290.3789 143.5308\r\n<\/pre>\n<pre><code class=\"r\">dcast(mcarsLong2, am + key ~ ., fun.aggregate=mean)\r\n<\/code><\/pre>\n<pre>##   am  key          .\r\n## 1  0  mpg  17.147368\r\n## 2  0  cyl   6.947368\r\n## 3  0 disp 290.378947\r\n## 4  1  mpg  24.392308\r\n## 5  1  cyl   5.076923\r\n## 6  1 disp 143.530769\r\n<\/pre>\n<h4>\ucc38\uace0\uc790\ub8cc {-}<\/h4>\n<p>R\uc5d0\ub294 <code>tidyr<\/code>\uc758 <code>gather<\/code>\/<code>spread<\/code>\uacfc <code>reshape2<\/code>\uc758 <code>melt<\/code>\/<code>dcast<\/code> \ud568\uc218 \uc678\uc5d0\ub3c4 \ub2e4\uc591\ud55c \ud568\uc218\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc138\ub85c\ud615\/\uac00\ub85c\ud615 \ubcc0\ud658\uacfc \uad00\ub828\ub41c \uc791\uc5c5\uc744 \ud560 \uc218 \uc788\ub2e4. \uc880 \ub354 \uc54c\uace0 \uc2f6\uc740 \ub3c5\uc790\ub294 \ub2e4\uc74c\uc758 \uc790\ub8cc\ub97c \ucc38\uace0\ud558\uae30 \ubc14\ub780\ub2e4.<\/p>\n<ul>\n<li><a href=\"http:\/\/141.164.34.82\/?p=930\">Long to Wide Form<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\ub370\uc774\ud130 \uc900\ube44 library(dplyr) library(tidyr) mtcars$name = rownames(mtcars); rownames(mtcars) = NULL mtcars %&gt;% select(name, am, mpg, cyl, disp) -&gt; mcars head(mcars, 4) ## name am mpg cyl disp ## 1 Mazda RX4 1 21.0 6 160 ## 2 Mazda RX4 Wag 1 21.0 6 160 ## 3 Datsun 710 1 22.8 4 108 ## 4 Hornet [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":957,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28,172,173,179],"tags":[177,176,175],"jetpack_featured_media_url":"http:\/\/ds.sumeun.org\/wp-content\/uploads\/2019\/02\/meltdcast.png","_links":{"self":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/948"}],"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=948"}],"version-history":[{"count":5,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/948\/revisions"}],"predecessor-version":[{"id":1018,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/948\/revisions\/1018"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/media\/957"}],"wp:attachment":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=948"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}