{"id":2047,"date":"2019-11-04T11:40:18","date_gmt":"2019-11-04T02:40:18","guid":{"rendered":"http:\/\/141.164.34.82\/?p=2047"},"modified":"2019-11-04T11:43:08","modified_gmt":"2019-11-04T02:43:08","slug":"wide-to-long-2","status":"publish","type":"post","link":"http:\/\/ds.sumeun.org\/?p=2047","title":{"rendered":"Wide to Long 2"},"content":{"rendered":"<pre><code class=\"r\">dat &lt;- read.table(header=T, text=&#39;\r\n name height.t1 weight.t1 height.t2 weight.t2 height.t3 weight.t3\r\n Sohn  158 49 166 56 170 60\r\n Mija  155 44 160 51 161 54\r\n James 159 50 165 55 169 59\r\n Mary  156 47 158 50 159 53\r\n &#39;)\r\nhead(dat)\r\n<\/code><\/pre>\n<pre>##    name height.t1 weight.t1 height.t2 weight.t2 height.t3 weight.t3\r\n## 1  Sohn       158        49       166        56       170        60\r\n## 2  Mija       155        44       160        51       161        54\r\n## 3 James       159        50       165        55       169        59\r\n## 4  Mary       156        47       158        50       159        53\r\n<\/pre>\n<h2><code>tidyr<\/code> \ubcf5\uc2b5<\/h2>\n<p>\uac00\ub85c\ud615\/\uc138\ub85c\ud615 \ubcc0\ud658\uc5d0\uc11c \uac00\uc7a5 \uc26c\uc6b4 \ubc29\ubc95\uc740 <code>tidyr::spread<\/code>\/<code>tidyr::gather<\/code>\uc778 \ub4ef \ubcf4\uc778\ub2e4. \uc138\ub85c\ud615\uc5d0\uc11c \uac00\ub85c\ud615\uc73c\ub85c \ub9cc\ub4e4 \ub54c\uc5d0\ub294 <code>gather<\/code>\uc744 \uc4f0\uace0, \ubc18\ub300\ub294 <code>spread<\/code>\ub97c \uc4f4\ub2e4. \uadf8\ub9ac\uace0 \uc5f4\uc774\ub984\uc744 \uc9c0\uc815\ud55c\ub2e4.<\/p>\n<pre><code class=\"r\">library(data.table)\r\nlibrary(dplyr)\r\nlibrary(tidyr)\r\ndat %&gt;% gather(key=&#39;key&#39;, value=&#39;value&#39;, height.t1:weight.t3)\r\n<\/code><\/pre>\n<pre>##     name       key value\r\n## 1   Sohn height.t1   158\r\n## 2   Mija height.t1   155\r\n## 3  James height.t1   159\r\n## 4   Mary height.t1   156\r\n## 5   Sohn weight.t1    49\r\n## 6   Mija weight.t1    44\r\n## 7  James weight.t1    50\r\n## 8   Mary weight.t1    47\r\n## 9   Sohn height.t2   166\r\n## 10  Mija height.t2   160\r\n## 11 James height.t2   165\r\n## 12  Mary height.t2   158\r\n## 13  Sohn weight.t2    56\r\n## 14  Mija weight.t2    51\r\n## 15 James weight.t2    55\r\n## 16  Mary weight.t2    50\r\n## 17  Sohn height.t3   170\r\n## 18  Mija height.t3   161\r\n## 19 James height.t3   169\r\n## 20  Mary height.t3   159\r\n## 21  Sohn weight.t3    60\r\n## 22  Mija weight.t3    54\r\n## 23 James weight.t3    59\r\n## 24  Mary weight.t3    53\r\n<\/pre>\n<pre><code class=\"r\">dat %&gt;% gather(key=&#39;key&#39;, value=&#39;value&#39;, height.t1:weight.t3) %&gt;%\r\n  spread(key=&#39;key&#39;, value=&#39;value&#39;)\r\n<\/code><\/pre>\n<pre>##    name height.t1 height.t2 height.t3 weight.t1 weight.t2 weight.t3\r\n## 1 James       159       165       169        50        55        59\r\n## 2  Mary       156       158       159        47        50        53\r\n## 3  Mija       155       160       161        44        51        54\r\n## 4  Sohn       158       166       170        49        56        60\r\n<\/pre>\n<h2>\uad6c\uc870\ud654\ub41c \uc5f4\uc774\ub984<\/h2>\n<p>\uc704\uc758 \uc5f4\uc774\ub984\uc744 \ubcf4\uba74 <strong>\ubc18\ubcf5<\/strong>\ub418\ub294 <strong>\ud328\ud134<\/strong>\uc744 \ud655\uc778\ud560 \uc218 \uc788\ub2e4. <code>height<\/code>\uc640 <code>weight<\/code>\ub294 \ud0a4\uc640 \uccb4\uc911\uc744 \ub098\ud0c0\ub0b4\uace0 <code>t1<\/code>, <code>t2<\/code>, <code>t3<\/code>\ub294 \uce21\uc815\ub41c \uc2dc\uac04\uc744 \ub098\ud0c0\ub0b8\ub2e4. \uc774\ub807\uac8c \uad6c\uc870\ud654\ub41c \uc5f4\uc774\ub984\uc740 \ub450 \uac00\uc9c0 \uc815\ubcf4(\uce21\uc815\ub41c \uac12, \uadf8\ub9ac\uace0 \uce21\uc815\ub41c \uc2dc\uac04)\uc744 \ub098\ud0c0\ub0b8\ub2e4.<\/p>\n<p>\uc5f4\uc774\ub984\uc5d0 \ud3ec\ud568\ub41c \uc815\ubcf4\ub97c \ubd84\ub9ac\ud558\ub824\uba74,<\/p>\n<pre><code class=\"r\">dat %&gt;% gather(key=&#39;key&#39;, value=&#39;value&#39;, height.t1:weight.t3) %&gt;%\r\n  separate(key, sep=&#39;[.]&#39;, into=c(&#39;var&#39;, &#39;time&#39;))\r\n<\/code><\/pre>\n<pre>##     name    var time value\r\n## 1   Sohn height   t1   158\r\n## 2   Mija height   t1   155\r\n## 3  James height   t1   159\r\n## 4   Mary height   t1   156\r\n## 5   Sohn weight   t1    49\r\n## 6   Mija weight   t1    44\r\n## 7  James weight   t1    50\r\n## 8   Mary weight   t1    47\r\n## 9   Sohn height   t2   166\r\n## 10  Mija height   t2   160\r\n## 11 James height   t2   165\r\n## 12  Mary height   t2   158\r\n## 13  Sohn weight   t2    56\r\n## 14  Mija weight   t2    51\r\n## 15 James weight   t2    55\r\n## 16  Mary weight   t2    50\r\n## 17  Sohn height   t3   170\r\n## 18  Mija height   t3   161\r\n## 19 James height   t3   169\r\n## 20  Mary height   t3   159\r\n## 21  Sohn weight   t3    60\r\n## 22  Mija weight   t3    54\r\n## 23 James weight   t3    59\r\n## 24  Mary weight   t3    53\r\n<\/pre>\n<p><code>key<\/code> \uc5f4\uc5d0 \ud3ec\ud568\ub41c \ub450 \uc815\ubcf4\ub97c <code>.<\/code>(\ub9c8\uce68\ud45c)\ub97c \uae30\uc900\uc73c\ub85c \ub450 \uc5f4 <code>var<\/code>\uacfc <code>time<\/code>\uc73c\ub85c \ub098\ub204\uc5c8\ub2e4. \uc774\ub54c <code>sep=<\/code>\uc5d0\ub294 RE(\uc815\uaddc\ud45c\ud604\uc2dd)\uc774 \uc4f0\uc784\uc744 \uc720\uc758\ud558\uc790.<\/p>\n<p>\uc774\ub807\uac8c \ub450 \uac1c\uc758 \uc5f4\uc744 \uc5bb\uc5c8\ub2e4\uba74 \uc774 \uc911 \ud55c \uc5f4\uc5d0 \ub300\ud574\uc11c \ub2e4\uc2dc \uac00\ub85c\ud615\uc73c\ub85c \ub9cc\ub4e4 \uc218 \uc788\ub2e4.<\/p>\n<pre><code class=\"r\">dat2 &lt;- dat %&gt;% \r\n  gather(key=&#39;key&#39;, value=&#39;value&#39;, height.t1:weight.t3) %&gt;%\r\n  separate(key, sep=&#39;[.]&#39;, into=c(&#39;var&#39;, &#39;time&#39;))\r\ndat2 %&gt;% spread(key=var, value=value)\r\n<\/code><\/pre>\n<pre>##     name time height weight\r\n## 1  James   t1    159     50\r\n## 2  James   t2    165     55\r\n## 3  James   t3    169     59\r\n## 4   Mary   t1    156     47\r\n## 5   Mary   t2    158     50\r\n## 6   Mary   t3    159     53\r\n## 7   Mija   t1    155     44\r\n## 8   Mija   t2    160     51\r\n## 9   Mija   t3    161     54\r\n## 10  Sohn   t1    158     49\r\n## 11  Sohn   t2    166     56\r\n## 12  Sohn   t3    170     60\r\n<\/pre>\n<pre><code class=\"r\">dat2 %&gt;% spread(key=&#39;time&#39;, value=value)\r\n<\/code><\/pre>\n<pre>##    name    var  t1  t2  t3\r\n## 1 James height 159 165 169\r\n## 2 James weight  50  55  59\r\n## 3  Mary height 156 158 159\r\n## 4  Mary weight  47  50  53\r\n## 5  Mija height 155 160 161\r\n## 6  Mija weight  44  51  54\r\n## 7  Sohn height 158 166 170\r\n## 8  Sohn weight  49  56  60\r\n<\/pre>\n<h2>Wide to Long 2<\/h2>\n<p>\ub9cc\uc57d \uad6c\uc870\ud654\ub41c \uc5f4\uc774\ub984\uc5d0\uc11c \ubc14\ub85c \uc704\uc758 \uc138\ub85c\ud615\uc73c\ub85c \ubcc0\ud658\ud558\uace0\uc790 \ud55c\ub2e4\uba74 \uc5b4\ub5bb\uac8c \ud574\uc57c \ud560\uae4c? <\/p>\n<p>\uadf8\ub7f0\ub370 \uadf8 \uc804\uc5d0 \uc65c \uadf8\ub807\uac8c, \ub2e4\uc2dc \uc911\uac04 \ub2e8\uacc4\ub97c \uac70\uce58\uc9c0 \uc54a\uace0 \ubc14\ub85c \ud574\uc57c \ud560\uae4c? \ubb3c\ub860 \uba54\ubaa8\ub9ac\uc640 \ub514\uc2a4\ud06c \uc6a9\ub7c9\uc758 \ubb38\uc81c \ub54c\ubb38\uc774\ub2e4. \ub9cc\uc57d \uc911\uac04\ub2e8\uacc4\ub97c \uac70\uccd0\uc57c \ud55c\ub2e4\uba74, \uad1c\ud788 \uba54\ubaa8\ub9ac\ub97c \uc7a1\uc544 \uba39\uac8c \ub418\uace0, \uc18d\ub3c4\uac00 \ub290\ub9ac\ub2e4. \uba54\ubaa8\ub9ac \ubd80\uc871\uc73c\ub85c \ubcc0\ud658\uc774 \ubd88\uac00\ub2a5\ud560 \uc218\ub3c4 \uc788\ub2e4.<\/p>\n<h3><code>stats::reshape<\/code><\/h3>\n<p>\ub180\ub78d\uac8c\ub3c4 R\uc758 \uae30\ubcf8 \ud328\ud0a4\uc9c0 <code>stats<\/code>\uc758 <code>reshape<\/code>\uc73c\ub85c \uc774\ub7f0 \uc791\uc5c5\uc744 \ud560 \uc218 \uc788\ub2e4.<\/p>\n<p>\uac00\uc7a5 \uae30\ubcf8\uc801\uc778 \ubc29\ubc95\uc740 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre><code class=\"r\">head(dat)\r\n<\/code><\/pre>\n<pre>##    name height.t1 weight.t1 height.t2 weight.t2 height.t3 weight.t3\r\n## 1  Sohn       158        49       166        56       170        60\r\n## 2  Mija       155        44       160        51       161        54\r\n## 3 James       159        50       165        55       169        59\r\n## 4  Mary       156        47       158        50       159        53\r\n<\/pre>\n<pre><code class=\"r\">reshape(dat, direction=&#39;long&#39;, varying=colnames(dat)[2:7])\r\n<\/code><\/pre>\n<pre>##       name time height weight id\r\n## 1.t1  Sohn   t1    158     49  1\r\n## 2.t1  Mija   t1    155     44  2\r\n## 3.t1 James   t1    159     50  3\r\n## 4.t1  Mary   t1    156     47  4\r\n## 1.t2  Sohn   t2    166     56  1\r\n## 2.t2  Mija   t2    160     51  2\r\n## 3.t2 James   t2    165     55  3\r\n## 4.t2  Mary   t2    158     50  4\r\n## 1.t3  Sohn   t3    170     60  1\r\n## 2.t3  Mija   t3    161     54  2\r\n## 3.t3 James   t3    169     59  3\r\n## 4.t3  Mary   t3    159     53  4\r\n<\/pre>\n<p>Voila! \ube44\ub85d \uc6b0\ub9ac\uac00 \uc6d0\ud558\ub358 <strong>\uae54\ub054\ud55c<\/strong> \ud615\ud0dc\ub294 \uc544\ub2c8\uc9c0\ub9cc \uae30\ubcf8\uc801\uc778 \uacb0\uacfc\ub294 \uc815\ud655\ud558\ub2e4.<\/p>\n<p>\uc5ec\uae30\uc11c \uba87 \uac00\uc9c0 \uac1c\uc120\uc744 \ud574\ubcf4\uc790.<\/p>\n<pre><code class=\"r\">reshape(dat, direction=&#39;long&#39;,  varying=colnames(dat)[2:7],\r\n        timevar=&#39;time&#39;,\r\n        times=c(&#39;t1&#39;, &#39;t2&#39;, &#39;t3&#39;),\r\n        idvar=&#39;name&#39;)\r\n<\/code><\/pre>\n<pre>##           name time height weight\r\n## Sohn.t1   Sohn   t1    158     49\r\n## Mija.t1   Mija   t1    155     44\r\n## James.t1 James   t1    159     50\r\n## Mary.t1   Mary   t1    156     47\r\n## Sohn.t2   Sohn   t2    166     56\r\n## Mija.t2   Mija   t2    160     51\r\n## James.t2 James   t2    165     55\r\n## Mary.t2   Mary   t2    158     50\r\n## Sohn.t3   Sohn   t3    170     60\r\n## Mija.t3   Mija   t3    161     54\r\n## James.t3 James   t3    169     59\r\n## Mary.t3   Mary   t3    159     53\r\n<\/pre>\n<h3><code>reshape<\/code>\uc758 \ud55c\uacc4 : \uacb0\uce21\uce58<\/h3>\n<p><code>reshape<\/code>\uc740 \uc5f4\uc774\ub984\uc758 \uc815\ubcf4\ub97c \uc801\uadf9\uc801\uc73c\ub85c \ud574\uc11d\ud558\uc9c0\ub294 \ubabb\ud558\ub294 \ub4ef \ud558\ub2e4. \ub2e4\uc74c\uc758 \ub370\uc774\ud130\ub97c \ubcf4\uc790. <code>weight.t2<\/code>\uac00 \uc874\uc7ac\ud558\uc9c0 \uc54a\ub294\ub2e4. <\/p>\n<pre><code class=\"r\">dat &lt;- read.table(header=T, text=&#39;\r\n name height.t1 weight.t1 height.t2 height.t3 weight.t3\r\n Sohn  158 49 166 170 60\r\n Mija  155 44 160 161 54\r\n James 159 50 165 169 59\r\n Mary  156 47 158 159 53\r\n &#39;)\r\nhead(dat)\r\n<\/code><\/pre>\n<pre>##    name height.t1 weight.t1 height.t2 height.t3 weight.t3\r\n## 1  Sohn       158        49       166       170        60\r\n## 2  Mija       155        44       160       161        54\r\n## 3 James       159        50       165       169        59\r\n## 4  Mary       156        47       158       159        53\r\n<\/pre>\n<pre><code class=\"r\">reshape(dat, direction=&#39;long&#39;,  varying=colnames(dat)[2:6],\r\n        timevar=&#39;time&#39;,\r\n        times=c(&#39;t1&#39;, &#39;t2&#39;, &#39;t3&#39;),\r\n        idvar=&#39;name&#39;)\r\n<\/code><\/pre>\n<pre>## Error in reshapeLong(data, idvar = idvar, timevar = timevar, varying = varying, : &#39;varying&#39; arguments must be the same length\r\n<\/pre>\n<p>\ubb3c\ub860 \ubc29\ubc95\uc774 \uc5c6\ub294 \uac83\uc740 \uc544\ub2c8\ub2e4.<\/p>\n<pre><code class=\"r\">dat$weight.t2 &lt;- NA\r\nreshape(dat, direction=&#39;long&#39;,  varying=colnames(dat)[2:7],\r\n        timevar=&#39;time&#39;,\r\n        times=c(&#39;t1&#39;, &#39;t2&#39;, &#39;t3&#39;),\r\n        idvar=&#39;name&#39;)\r\n<\/code><\/pre>\n<pre>##           name time height weight\r\n## Sohn.t1   Sohn   t1    158     49\r\n## Mija.t1   Mija   t1    155     44\r\n## James.t1 James   t1    159     50\r\n## Mary.t1   Mary   t1    156     47\r\n## Sohn.t2   Sohn   t2    166     60\r\n## Mija.t2   Mija   t2    160     54\r\n## James.t2 James   t2    165     59\r\n## Mary.t2   Mary   t2    158     53\r\n## Sohn.t3   Sohn   t3    170     NA\r\n## Mija.t3   Mija   t3    161     NA\r\n## James.t3 James   t3    169     NA\r\n## Mary.t3   Mary   t3    159     NA\r\n<\/pre>\n<p>\ud558\uc9c0\ub9cc \ubb54\uac00 \uc774\uc0c1\ud558\ub2e4. <code>t2.weight<\/code>\uac00 \uacb0\uce21\uce58\uc778\ub370, \uc704\uc5d0\uc11c\ub294 <code>t3.weight<\/code>\uc5d0 \uacb0\uce21\uce58\uac00 \uc788\ub2e4. \uc774\ub7f0 \uacb0\uacfc\ub85c \ubcf4\uc544 <code>reshape<\/code>\uc744 \ubaa8\ub4e0 \uacbd\uc6b0(\uc77c\ubc18\uc801\uc778 \uacbd\uc6b0)\uc5d0 \uc5b8\uc81c\ub098 \uc815\ud655\ud558\uac8c \uc791\ub3d9\ud560 \uac83\uc73c\ub85c \uae30\ub300\ud558\uae30\ub294 \ubd80\uc871\ud55c \ub4ef \ud558\ub2e4.  <\/p>\n<p>\ub2e4\uc74c\uc758 \ub300\uc548\uc774 \uc788\uae30\ub294 \ud558\uc9c0\ub9cc&hellip;<\/p>\n<pre><code class=\"r\">reshape(dat, direction=&#39;long&#39;, \r\n        varying=list(c(&#39;height.t1&#39;, &#39;height.t2&#39;, &#39;height.t3&#39;),\r\n                     c(&#39;weight.t1&#39;, &#39;weight.t2&#39;, &#39;weight.t3&#39;)),\r\n        idvar=&#39;name&#39;, \r\n        v.names=c(&#39;height&#39;, &#39;weight&#39;),\r\n        timevar=&#39;time&#39;,\r\n        times=c(&#39;t1&#39;, &#39;t2&#39;, &#39;t3&#39;))\r\n<\/code><\/pre>\n<pre>##           name time height weight\r\n## Sohn.t1   Sohn   t1    158     49\r\n## Mija.t1   Mija   t1    155     44\r\n## James.t1 James   t1    159     50\r\n## Mary.t1   Mary   t1    156     47\r\n## Sohn.t2   Sohn   t2    166     NA\r\n## Mija.t2   Mija   t2    160     NA\r\n## James.t2 James   t2    165     NA\r\n## Mary.t2   Mary   t2    158     NA\r\n## Sohn.t3   Sohn   t3    170     60\r\n## Mija.t3   Mija   t3    161     54\r\n## James.t3 James   t3    169     59\r\n## Mary.t3   Mary   t3    159     53\r\n<\/pre>\n<pre><code class=\"r\">dat$weight.t2 &lt;- NA\r\nreshape(dat, direction=&#39;long&#39;, \r\n        varying=list(c(&#39;height.t1&#39;, &#39;weight.t1&#39;),\r\n                     c(&#39;height.t2&#39;, &#39;weight.t2&#39;),\r\n                     c(&#39;height.t3&#39;, &#39;weight.t3&#39;)),\r\n        idvar=&#39;name&#39;, \r\n        v.names=c(&#39;t1&#39;, &#39;t2&#39;, &#39;t3&#39;),\r\n        timevar=&#39;var&#39;,\r\n        times=c(&#39;height&#39;, &#39;weight&#39;))\r\n<\/code><\/pre>\n<pre>##               name    var  t1  t2  t3\r\n## Sohn.height   Sohn height 158 166 170\r\n## Mija.height   Mija height 155 160 161\r\n## James.height James height 159 165 169\r\n## Mary.height   Mary height 156 158 159\r\n## Sohn.weight   Sohn weight  49  NA  60\r\n## Mija.weight   Mija weight  44  NA  54\r\n## James.weight James weight  50  NA  59\r\n## Mary.weight   Mary weight  47  NA  53\r\n<\/pre>\n<h4>\ubb34\uc5c7\uc744 \ub0a8\uae38 \uac83\uc778\uac00?<\/h4>\n<p>\uc774 \ubd80\ubd84\uc740 \uc774\ub807\uac8c \uc774\ud574\ud560 \uc788\ub2e4. \ubcf4\ud1b5 \uc138\ub85c\ud615\uc740 \uc2dc\uac04 \ubcc0\uc218\ub97c \uc138\ub85c\ud615\uc73c\ub85c \ub9cc\ub4e0\ub2e4. \uac00\ub85c\ud615\uc5d0\uc11c \uc138\ub85c\ud615\uc73c\ub85c \ubc14\uafc0 \ubcc0\uc218(<code>varying=<\/code>), \uc138\ub85c\ud615\uc73c\ub85c \ubcc0\ud658 \ub41c \uc5f4\uc758 \uc774\ub984(<code>timevar=<\/code>), \uadf8\ub9ac\uace0 \uadf8 \ub0b4\uc6a9(<code>times=<\/code>)\uc744 \uc815\ud574\uc8fc\uc5c8\ub2e4\uba74 \uae30\ubcf8\uc801\uc778 \ud2c0\uc744 \uac16\ucd94\uc5b4\uc9c4 \uc148\uc774\ub2e4. <\/p>\n<pre><code class=\"r\">reshape(dat, direction=&#39;long&#39;, \r\n        varying=list(c(&#39;height.t1&#39;, &#39;weight.t1&#39;),\r\n                     c(&#39;height.t2&#39;, &#39;weight.t2&#39;),\r\n                     c(&#39;height.t3&#39;, &#39;weight.t3&#39;)),\r\n        timevar=&#39;var&#39;, \r\n        times=c(&#39;height&#39;, &#39;weight&#39;))\r\n<\/code><\/pre>\n<pre>##           name    var height.t1 height.t2 height.t3 id\r\n## 1.height  Sohn height       158       166       170  1\r\n## 2.height  Mija height       155       160       161  2\r\n## 3.height James height       159       165       169  3\r\n## 4.height  Mary height       156       158       159  4\r\n## 1.weight  Sohn weight        49        NA        60  1\r\n## 2.weight  Mija weight        44        NA        54  2\r\n## 3.weight James weight        50        NA        59  3\r\n## 4.weight  Mary weight        47        NA        53  4\r\n<\/pre>\n<h3><code>data.table::melt<\/code><\/h3>\n<p><code>data.table<\/code>\uc758 <code>melt<\/code>\uc740 \ub300\uc548\uc774 \ub420 \uc218 \uc788\ub2e4. <code>data.table::melt<\/code>\uc744 \uc0ac\uc6a9\ud558\ub824\uba74 \uba3c\uc800 \ub370\uc774\ud130\ub97c \ub370\uc774\ud130\ud14c\uc774\ube14\ub85c \ubcc0\ud658\ud55c \ud6c4, \ubb34\uc5c7\uc744 \uac00\ub85c\ud615\uc73c\ub85c \ub0a8\uae38\uc9c0 \uacb0\uc815\ud55c\ub2e4. <\/p>\n<pre><code class=\"r\">DT &lt;- data.table(dat)\r\nmelt(DT, measure=patterns(&#39;t1&#39;, &#39;t2&#39;, &#39;t3&#39;))\r\n<\/code><\/pre>\n<pre>## Warning in melt.data.table(DT, measure = patterns(&quot;t1&quot;, &quot;t2&quot;, &quot;t3&quot;)):\r\n## &#39;measure.vars&#39; [height.t2, weight.t2] are not all of the same type. By\r\n## order of hierarchy, the molten data value column will be of type &#39;integer&#39;.\r\n## All measure variables not of type &#39;integer&#39; will be coerced too. Check\r\n## DETAILS in ?melt.data.table for more on coercion.\r\n<\/pre>\n<pre>##     name variable value1 value2 value3\r\n## 1:  Sohn        1    158    166    170\r\n## 2:  Mija        1    155    160    161\r\n## 3: James        1    159    165    169\r\n## 4:  Mary        1    156    158    159\r\n## 5:  Sohn        2     49     NA     60\r\n## 6:  Mija        2     44     NA     54\r\n## 7: James        2     50     NA     59\r\n## 8:  Mary        2     47     NA     53\r\n<\/pre>\n<pre><code class=\"r\">melt(DT, measure=patterns(&#39;height&#39;, &#39;weight&#39;))\r\n<\/code><\/pre>\n<pre>## Warning in melt.data.table(DT, measure = patterns(&quot;height&quot;, &quot;weight&quot;)):\r\n## &#39;measure.vars&#39; [weight.t1, weight.t3, weight.t2] are not all of the same\r\n## type. By order of hierarchy, the molten data value column will be of type\r\n## &#39;integer&#39;. All measure variables not of type &#39;integer&#39; will be coerced too.\r\n## Check DETAILS in ?melt.data.table for more on coercion.\r\n<\/pre>\n<pre>##      name variable value1 value2\r\n##  1:  Sohn        1    158     49\r\n##  2:  Mija        1    155     44\r\n##  3: James        1    159     50\r\n##  4:  Mary        1    156     47\r\n##  5:  Sohn        2    166     60\r\n##  6:  Mija        2    160     54\r\n##  7: James        2    165     59\r\n##  8:  Mary        2    158     53\r\n##  9:  Sohn        3    170     NA\r\n## 10:  Mija        3    161     NA\r\n## 11: James        3    169     NA\r\n## 12:  Mary        3    159     NA\r\n<\/pre>\n<p><code>variable<\/code>\uc758 \ub0b4\uc6a9\uc774\ub098, <code>value1<\/code>, <code>value2<\/code>\ub294 \uc801\uc808\ud558\uac8c \uc9c0\uc815\ud574\uc900\ub2e4.<\/p>\n<pre><code class=\"r\">melt(DT, measure=patterns(&#39;height&#39;, &#39;weight&#39;),\r\n     variable.name=&#39;time&#39;, \r\n     value.name=c(&#39;height&#39;, &#39;weight&#39;))\r\n<\/code><\/pre>\n<pre>## Warning in melt.data.table(DT, measure = patterns(&quot;height&quot;, &quot;weight&quot;),\r\n## variable.name = &quot;time&quot;, : &#39;measure.vars&#39; [weight.t1, weight.t3, weight.t2]\r\n## are not all of the same type. By order of hierarchy, the molten data\r\n## value column will be of type &#39;integer&#39;. All measure variables not of type\r\n## &#39;integer&#39; will be coerced too. Check DETAILS in ?melt.data.table for more\r\n## on coercion.\r\n<\/pre>\n<pre>##      name time height weight\r\n##  1:  Sohn    1    158     49\r\n##  2:  Mija    1    155     44\r\n##  3: James    1    159     50\r\n##  4:  Mary    1    156     47\r\n##  5:  Sohn    2    166     60\r\n##  6:  Mija    2    160     54\r\n##  7: James    2    165     59\r\n##  8:  Mary    2    158     53\r\n##  9:  Sohn    3    170     NA\r\n## 10:  Mija    3    161     NA\r\n## 11: James    3    169     NA\r\n## 12:  Mary    3    159     NA\r\n<\/pre>\n<p>\ud558\uc9c0\ub9cc \uacb0\uce21\uce58\uac00 \uc788\uc744 \uacbd\uc6b0\uc5d0\ub294 \uc81c\ub300\ub85c \uc791\ub3d9\ud558\uc9c0 \uc54a\ub294\ub2e4. \uc544\ub9c8\ub3c4 pattern\uc5d0 \uba85\uc2dc\ub41c \uc774\ub984\uc744 \uc81c\uc678\ud558\uace0\ub294 <strong>\uc21c\uc11c<\/strong>\ub9cc \uace0\ub824\ud558\ub294 \ub4ef \ud558\ub2e4.<\/p>\n<pre><code class=\"r\">DT &lt;- data.table(dat)\r\nDT[ , weight.t2:=NULL]\r\nDTlong1 &lt;- melt(DT, measure=patterns(&#39;t1&#39;, &#39;t2&#39;, &#39;t3&#39;))\r\nDTlong1\r\n<\/code><\/pre>\n<pre>##     name variable value1 value2 value3\r\n## 1:  Sohn        1    158    166    170\r\n## 2:  Mija        1    155    160    161\r\n## 3: James        1    159    165    169\r\n## 4:  Mary        1    156    158    159\r\n## 5:  Sohn        2     49     NA     60\r\n## 6:  Mija        2     44     NA     54\r\n## 7: James        2     50     NA     59\r\n## 8:  Mary        2     47     NA     53\r\n<\/pre>\n<pre><code class=\"r\">DTlong2 &lt;- melt(DT, measure=patterns(&#39;height&#39;, &#39;weight&#39;))\r\nDTlong2\r\n<\/code><\/pre>\n<pre>##      name variable value1 value2\r\n##  1:  Sohn        1    158     49\r\n##  2:  Mija        1    155     44\r\n##  3: James        1    159     50\r\n##  4:  Mary        1    156     47\r\n##  5:  Sohn        2    166     60\r\n##  6:  Mija        2    160     54\r\n##  7: James        2    165     59\r\n##  8:  Mary        2    158     53\r\n##  9:  Sohn        3    170     NA\r\n## 10:  Mija        3    161     NA\r\n## 11: James        3    169     NA\r\n## 12:  Mary        3    159     NA\r\n<\/pre>\n<p><code>variable<\/code>\uc758 \ub0b4\uc6a9\uc744 \uc9c1\uc811 \ubcc0\uacbd\ud560 \uc218 \uc788\ub294 \ubc29\ubc95\uc774 \uc5c6\uc74c\uc774 \uc57d\uac04 \uc544\uc27d\ub2e4.<\/p>\n<pre><code class=\"r\">DTlong1[, variable:=c(&#39;height&#39;, &#39;weight&#39;)[variable]]\r\nDTlong2\r\n<\/code><\/pre>\n<pre>##      name variable value1 value2\r\n##  1:  Sohn        1    158     49\r\n##  2:  Mija        1    155     44\r\n##  3: James        1    159     50\r\n##  4:  Mary        1    156     47\r\n##  5:  Sohn        2    166     60\r\n##  6:  Mija        2    160     54\r\n##  7: James        2    165     59\r\n##  8:  Mary        2    158     53\r\n##  9:  Sohn        3    170     NA\r\n## 10:  Mija        3    161     NA\r\n## 11: James        3    169     NA\r\n## 12:  Mary        3    159     NA\r\n<\/pre>\n<pre><code class=\"r\">DTlong2[ , variable:=c(&#39;t1&#39;, &#39;t2&#39;, &#39;t3&#39;)[variable]] \r\nDTlong2\r\n<\/code><\/pre>\n<pre>##      name variable value1 value2\r\n##  1:  Sohn       t1    158     49\r\n##  2:  Mija       t1    155     44\r\n##  3: James       t1    159     50\r\n##  4:  Mary       t1    156     47\r\n##  5:  Sohn       t2    166     60\r\n##  6:  Mija       t2    160     54\r\n##  7: James       t2    165     59\r\n##  8:  Mary       t2    158     53\r\n##  9:  Sohn       t3    170     NA\r\n## 10:  Mija       t3    161     NA\r\n## 11: James       t3    169     NA\r\n## 12:  Mary       t3    159     NA\r\n<\/pre>\n<h2>\uacb0\ub860<\/h2>\n<p>\uad6c\uc870\ud654\ub41c \uc5f4\uc774\ub984\uc744 \uac00\uc9c0\uace0 \uc788\uc744 \uacbd\uc6b0\uc5d0 \uac00\ub85c\ud615\uc73c\ub85c \ub0a8\uae38 \ubcc0\uc218\ub97c \uc9c0\uc815\ud558\uc5ec \ub2e4\uc74c\uacfc \uac19\uc774 \uc0ac\uc6a9\ud558\ub294 \uac83\uc774 \uac00\uc7a5 \uac04\ub2e8\ud574\ubcf4\uc778\ub2e4. (<code>pattern=<\/code>\uc774 \uc544\ub2c8\ub77c <code>patterns=<\/code>\uc784\uc744 \uc720\uc758\ud558\uc790.)<\/p>\n<pre><code class=\"r\">data.table::melt(DT, measure=patterns(&#39;t1&#39;, &#39;t2&#39;, &#39;t3&#39;))\r\n<\/code><\/pre>\n<pre>##     name variable value1 value2 value3\r\n## 1:  Sohn        1    158    166    170\r\n## 2:  Mija        1    155    160    161\r\n## 3: James        1    159    165    169\r\n## 4:  Mary        1    156    158    159\r\n## 5:  Sohn        2     49     NA     60\r\n## 6:  Mija        2     44     NA     54\r\n## 7: James        2     50     NA     59\r\n## 8:  Mary        2     47     NA     53\r\n<\/pre>\n<p>\uc774\ub54c \uc8fc\uc5b4\uc9c4 \uc790\ub8cc\ub97c \ub370\uc774\ud130\ud14c\uc774\ube14\ub85c \ub9cc\ub4e4\uace0, <strong>\uc138\ub85c\ud615\uc774 \ub420 \ubcc0\uc218\ub294 \uc21c\uc11c\uc640 \uac2f\uc218\ub97c \uc77c\uc815\ud558\uac8c \ub9de\ucd94\uc5b4\uc8fc\uc5b4\uc57c \ud568\uc744 \uc78a\uc9c0 \ub9d0\uc790!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>dat &lt;- read.table(header=T, text=&#39; name height.t1 weight.t1 height.t2 weight.t2 height.t3 weight.t3 Sohn 158 49 166 56 170 60 Mija 155 44 160 51 161 54 James 159 50 165 55 169 59 Mary 156 47 158 50 159 53 &#39;) head(dat) ## name height.t1 weight.t1 height.t2 weight.t2 height.t3 weight.t3 ## 1 Sohn 158 49 166 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2049,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28,172,174,30,173],"tags":[183,176,413],"jetpack_featured_media_url":"http:\/\/ds.sumeun.org\/wp-content\/uploads\/2019\/11\/longToWide2.png","_links":{"self":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/2047"}],"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=2047"}],"version-history":[{"count":1,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/2047\/revisions"}],"predecessor-version":[{"id":2048,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/2047\/revisions\/2048"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/media\/2049"}],"wp:attachment":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2047"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}