{"id":2273,"date":"2021-01-24T03:04:13","date_gmt":"2021-01-23T18:04:13","guid":{"rendered":"http:\/\/141.164.34.82\/?p=2273"},"modified":"2021-01-25T11:12:07","modified_gmt":"2021-01-25T02:12:07","slug":"%ea%b0%80%eb%a1%9c-%ec%84%b8%eb%a1%9c%ed%98%95","status":"publish","type":"post","link":"http:\/\/ds.sumeun.org\/?p=2273","title":{"rendered":"\ubd80\ubd84 \uac00\ub85c\/\uc138\ub85c\ud615"},"content":{"rendered":"<h2><code>melt<\/code>&#8211;<code>dcast<\/code> \uc870\ud569<\/h2>\n<p>\uac00\ub85c\/\uc138\ub85c\ud615 \ubcc0\ud658\uc5d0\uc11c \uac00\uc7a5 \uc26c\uc6b4 \ubc29\ubc95\uc740 <code>gather<\/code>\/<code>spread<\/code> \uc870\ud569\uc77c \uac83\uc774\ub2e4. \ud558\uc9c0\ub9cc \uc644\uc804\ud55c \uc138\ub85c\ud615\/\uac00\ub85c\ud615\uc774 \uc544\ub2c8\ub77c \uc77c\ubd80 \uac00\ub85c\/\uc138\ub85c\ud615\uc758 \uacbd\uc6b0\uc5d0\ub294 <code>melt<\/code>\/<code>dcast<\/code> \uc870\ud569\uc744 \uc0ac\uc6a9\ud560 \ud544\uc694\uac00 \uc788\ub2e4.<\/p>\n<pre><code class=\"r\">library(dplyr)\r\nlibrary(reshape2)\r\n<\/code><\/pre>\n<h3>\ub370\uc774\ud130<\/h3>\n<pre><code class=\"r\">x1s &lt;- c('A', 'B', 'C')\r\nx2s &lt;- c('x', 'y', 'z', 'w')\r\nx3s &lt;- c('F1', 'F2', 'F3', 'F4')\r\ndf &lt;- expand.grid(x1 = factor(x1s),\r\n                  x2 = factor(x2s),\r\n                  x3 = factor(x3s))\r\n\r\nv1s &lt;- rnorm(length(x1s))\r\nv2s &lt;- rnorm(length(x2s))\r\nv3s &lt;- rnorm(length(x3s))\r\n\r\ndf &lt;- df %&gt;% mutate(value = v1s[as.numeric(x1)] + \r\n                      v2s[as.numeric(x2)] + \r\n                      v3s[as.numeric(x3)] + rnorm(nrow(df)))   \r\n# df$value &lt;- \r\n#   v1s[as.numeric(df$x1)] + \r\n#   v2s[as.numeric(df$x2)] + \r\n#   v3s[as.numeric(df$x3)]\r\n\r\nhead(df)\r\n<\/code><\/pre>\n<pre>##   x1 x2 x3     value\r\n## 1  A  x F1 -3.687556\r\n## 2  B  x F1 -2.425758\r\n## 3  C  x F1 -4.196482\r\n## 4  A  y F1  1.092759\r\n## 5  B  y F1  3.354659\r\n## 6  C  y F1  1.776936\r\n<\/pre>\n<p><code>df<\/code>\uc5d0\uc11c <code>x1<\/code>, <code>x2<\/code>, <code>x3<\/code>\ub294 \uc870\uac74\uc5d0 \ud574\ub2f9\ud558\uace0 <code>value<\/code>\ub294 \uac12\uc744 \ub098\ud0c0\ub0b8\ub2e4. \ud558\uc9c0\ub9cc <code>x3<\/code>\uc744 \ub2e4\ub978 \uce21\uc815\uac12\uc73c\ub85c \uc0dd\uac01\ud574\ub3c4 \ubb34\ubc29\ud558\ub2e4. \uc608\ub97c \ub4e4\uba74 <code>x3<\/code>\uc758 <code>F1<\/code>, <code>F2<\/code>, <code>F3<\/code>, <code>F4<\/code>\ub97c <code>height<\/code>, <code>weight<\/code>, <code>sight<\/code>, <code>BMI<\/code> \ub4f1\uc73c\ub85c \uc0dd\uac01\ud558\ub294 \uac83\uc774\ub2e4.<\/p>\n<pre><code class=\"r\">require(dplyr)\r\ndf &lt;- df %&gt;% \r\n  mutate(x3 = ifelse(x3==\"F1\", 'height',\r\n                     ifelse(x3==\"F2\", 'weight',\r\n                            ifelse(x3==\"F3\", 'sight', 'BMI'))))\r\nhead(df)     \r\n<\/code><\/pre>\n<pre>##   x1 x2     x3     value\r\n## 1  A  x height -3.687556\r\n## 2  B  x height -2.425758\r\n## 3  C  x height -4.196482\r\n## 4  A  y height  1.092759\r\n## 5  B  y height  3.354659\r\n## 6  C  y height  1.776936\r\n<\/pre>\n<p>\uc774\ub807\uac8c \uc644\uc804\ud55c \uc138\ub85c\ud615\uc77c \uacbd\uc6b0, \uc77c\ubd80 \uac00\ub85c\ud615\uc73c\ub85c \ub9cc\ub4dc\ub294 \uac83\uc740 \uc27d\ub2e4. \uadf8\ub0e5 \uac00\ub85c\ud615\uc73c\ub85c \ub9cc\ub4e4 \ubcc0\uc218\ub97c \ub2e4\uc74c\uc758 <code>dcast<\/code> \uacf5\uc2dd <code>~<\/code>\uc758 \uc624\ub978\ucabd\uc5d0 \uc801\uc5b4\uc8fc\uba74 \ub41c\ub2e4.<\/p>\n<pre><code class=\"r\">require(reshape2)\r\ndf2 = dcast(df, x1~x2+x3)\r\n<\/code><\/pre>\n<pre>## Warning in dcast(df, x1 ~ x2 + x3): The dcast generic in data.table has been passed a data.frame and will\r\n## attempt to redirect to the reshape2::dcast; please note that reshape2 is deprecated, and this redirection is\r\n## now deprecated as well. Please do this redirection yourself like reshape2::dcast(df). In the next version, this\r\n## warning will become an error.\r\n<\/pre>\n<pre><code class=\"r\">head(df2)\r\n<\/code><\/pre>\n<pre>##   x1     w_BMI     w_height  w_sight  w_weight      x_BMI  x_height    x_sight   x_weight    y_BMI y_height\r\n## 1  A 0.2497362 -0.002433557 1.626930 1.6627601 -2.6162418 -3.687556 -0.8271366 -2.2951541 2.399217 1.092759\r\n## 2  B 2.1830400  1.370493919 1.948437 1.6044455 -0.2824331 -2.425758 -0.7175963  0.9055963 3.539909 3.354659\r\n## 3  C 0.1299226 -0.103315795 3.650791 0.5405934 -1.2623258 -4.196482 -1.4819258 -3.1036973 3.160218 1.776936\r\n##    y_sight   y_weight      z_BMI   z_height    z_sight    z_weight\r\n## 1 2.792275 -0.2188165 -2.8345123 -1.7443293  0.5987874 -0.39041870\r\n## 2 2.967254  3.4699891 -1.1376816  0.8465476  2.0376010  1.27049918\r\n## 3 3.699912  1.0492078 -0.3630513 -3.0043727 -1.4373376  0.06806562\r\n<\/pre>\n<pre><code class=\"r\">df3 = dcast(df, x1+x2~x3)\r\n<\/code><\/pre>\n<pre>## Warning in dcast(df, x1 + x2 ~ x3): The dcast generic in data.table has been passed a data.frame and will\r\n## attempt to redirect to the reshape2::dcast; please note that reshape2 is deprecated, and this redirection is\r\n## now deprecated as well. Please do this redirection yourself like reshape2::dcast(df). In the next version, this\r\n## warning will become an error.\r\n<\/pre>\n<pre><code class=\"r\">head(df3)\r\n<\/code><\/pre>\n<pre>##   x1 x2        BMI       height      sight     weight\r\n## 1  A  w  0.2497362 -0.002433557  1.6269303  1.6627601\r\n## 2  A  x -2.6162418 -3.687555600 -0.8271366 -2.2951541\r\n## 3  A  y  2.3992172  1.092758736  2.7922748 -0.2188165\r\n## 4  A  z -2.8345123 -1.744329327  0.5987874 -0.3904187\r\n## 5  B  w  2.1830400  1.370493919  1.9484374  1.6044455\r\n## 6  B  x -0.2824331 -2.425758012 -0.7175963  0.9055963\r\n<\/pre>\n<p>\uc8fc\uc5b4\uc9c4 \ub370\uc774\ud130\uac00 \uc644\uc804\ud55c \uc138\ub85c\ud615\uc774 \uc544\ub2c8\ub77c\uba74 \uc6b0\uc120 \uc644\uc804\ud55c \uc138\ub85c\ud615\uc73c\ub85c \ub9cc\ub4e4\uba74 \ub41c\ub2e4. <code>melt<\/code>\ub97c \ud65c\uc6a9\ud558\uc790.<\/p>\n<pre><code class=\"r\">df2_melted1 &lt;- melt(df2, id.vars='x1')\r\n<\/code><\/pre>\n<pre>## Warning in melt(df2, id.vars = \"x1\"): The melt generic in data.table has been passed a data.frame and will\r\n## attempt to redirect to the relevant reshape2 method; please note that reshape2 is deprecated, and this\r\n## redirection is now deprecated as well. To continue using melt methods from reshape2 while both libraries are\r\n## attached, e.g. melt.list, you can prepend the namespace like reshape2::melt(df2). In the next version, this\r\n## warning will become an error.\r\n<\/pre>\n<pre><code class=\"r\">require(tidyr)\r\ndf2_melted2 &lt;- melt(df2, id.vars='x1') %&gt;% separate(col='variable', sep='_', into=c('x2', 'x3'))\r\n<\/code><\/pre>\n<pre>## Warning in melt(df2, id.vars = \"x1\"): The melt generic in data.table has been passed a data.frame and will\r\n## attempt to redirect to the relevant reshape2 method; please note that reshape2 is deprecated, and this\r\n## redirection is now deprecated as well. To continue using melt methods from reshape2 while both libraries are\r\n## attached, e.g. melt.list, you can prepend the namespace like reshape2::melt(df2). In the next version, this\r\n## warning will become an error.\r\n<\/pre>\n<pre><code class=\"r\">head(df2_melted2)\r\n<\/code><\/pre>\n<pre>##   x1 x2     x3        value\r\n## 1  A  w    BMI  0.249736187\r\n## 2  B  w    BMI  2.183040009\r\n## 3  C  w    BMI  0.129922564\r\n## 4  A  w height -0.002433557\r\n## 5  B  w height  1.370493919\r\n## 6  C  w height -0.103315795\r\n<\/pre>\n<pre><code class=\"r\">df3_melted1 &lt;- melt(df3, id.vars='x1')\r\n<\/code><\/pre>\n<pre>## Warning in melt(df3, id.vars = \"x1\"): The melt generic in data.table has been passed a data.frame and will\r\n## attempt to redirect to the relevant reshape2 method; please note that reshape2 is deprecated, and this\r\n## redirection is now deprecated as well. To continue using melt methods from reshape2 while both libraries are\r\n## attached, e.g. melt.list, you can prepend the namespace like reshape2::melt(df3). In the next version, this\r\n## warning will become an error.\r\n<\/pre>\n<pre>## Warning: attributes are not identical across measure variables; they will be dropped\r\n<\/pre>\n<pre><code class=\"r\">df3_melted2 &lt;- melt(df3, variable.name = 'x3', value.name='value', id.vars=c('x1', 'x2'))\r\n<\/code><\/pre>\n<pre>## Warning in melt(df3, variable.name = \"x3\", value.name = \"value\", id.vars = c(\"x1\", : The melt generic in\r\n## data.table has been passed a data.frame and will attempt to redirect to the relevant reshape2 method; please\r\n## note that reshape2 is deprecated, and this redirection is now deprecated as well. To continue using melt\r\n## methods from reshape2 while both libraries are attached, e.g. melt.list, you can prepend the namespace like\r\n## reshape2::melt(df3). In the next version, this warning will become an error.\r\n<\/pre>\n<pre><code class=\"r\"># gather\uc5d0\uc11c key, value\uacfc variable.name, value.name\uc73c\ub85c \ubc14\ub00c\uc5c8\ub2e4. \r\nhead(df3_melted2)\r\n<\/code><\/pre>\n<pre>##   x1 x2  x3      value\r\n## 1  A  w BMI  0.2497362\r\n## 2  A  x BMI -2.6162418\r\n## 3  A  y BMI  2.3992172\r\n## 4  A  z BMI -2.8345123\r\n## 5  B  w BMI  2.1830400\r\n## 6  B  x BMI -0.2824331\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>melt&#8211;dcast \uc870\ud569 \uac00\ub85c\/\uc138\ub85c\ud615 \ubcc0\ud658\uc5d0\uc11c \uac00\uc7a5 \uc26c\uc6b4 \ubc29\ubc95\uc740 gather\/spread \uc870\ud569\uc77c \uac83\uc774\ub2e4. \ud558\uc9c0\ub9cc \uc644\uc804\ud55c \uc138\ub85c\ud615\/\uac00\ub85c\ud615\uc774 \uc544\ub2c8\ub77c \uc77c\ubd80 \uac00\ub85c\/\uc138\ub85c\ud615\uc758 \uacbd\uc6b0\uc5d0\ub294 melt\/dcast \uc870\ud569\uc744 \uc0ac\uc6a9\ud560 \ud544\uc694\uac00 \uc788\ub2e4. library(dplyr) library(reshape2) \ub370\uc774\ud130 x1s &lt;- c(&#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;) x2s &lt;- c(&#8216;x&#8217;, &#8216;y&#8217;, &#8216;z&#8217;, &#8216;w&#8217;) x3s &lt;- c(&#8216;F1&#8217;, &#8216;F2&#8217;, &#8216;F3&#8217;, &#8216;F4&#8217;) df &lt;- expand.grid(x1 = factor(x1s), x2 = factor(x2s), x3 = factor(x3s)) v1s &lt;- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2277,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31,28],"tags":[118,475,476,119],"jetpack_featured_media_url":"http:\/\/ds.sumeun.org\/wp-content\/uploads\/2021\/01\/melt_dcast.png","_links":{"self":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/2273"}],"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=2273"}],"version-history":[{"count":4,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/2273\/revisions"}],"predecessor-version":[{"id":2278,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/2273\/revisions\/2278"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/media\/2277"}],"wp:attachment":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2273"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}