{"id":838,"date":"2019-01-24T12:23:39","date_gmt":"2019-01-24T03:23:39","guid":{"rendered":"http:\/\/141.164.34.82\/?p=838"},"modified":"2022-02-03T17:39:26","modified_gmt":"2022-02-03T08:39:26","slug":"dplyr-02-modifying","status":"publish","type":"post","link":"http:\/\/ds.sumeun.org\/?p=838","title":{"rendered":"\ud328\ud0a4\uc9c0 dplyr 02: \uc218\uc815"},"content":{"rendered":"<h1><code>dplyr<\/code>\uc758 \ubc29\uc2dd : \uc218\uc815<\/h1>\n<h3>\uc0c8\ub85c\uc6b4 \uc5f4 \ucd94\uac00<\/h3>\n<ul>\n<li>\uc0c8\ub85c\uc6b4 \uc5f4\uc744 \ucd94\uac00\ud558\uace0\uc790 \ud55c\ub2e4\uba74 <code>mutate<\/code> \ud568\uc218\ub97c \uc0ac\uc6a9\ud55c\ub2e4. \uc5f4\uc774\ub984\uc740 \uc815\ud558\uac70\ub098 \uc0dd\ub7b5\ud560 \uc218 \uc788\ub2e4. \uc5ec\ub7ec \uc5f4\uc744 \ud568\uaed8 \ucd94\uac00\ud560 \uc218\ub3c4 \uc788\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">library(dplyr)\r\ndata(mtcars) \r\ntb = as_tibble(mtcars) \r\n\r\ntb2 &lt;- tb %&gt;% select(hp, cyl, qsec) %&gt;% slice(1:3)\r\n\r\ntb2 %&gt;% mutate(hp\/cyl)\r\n<\/code><\/pre>\n<pre>## # A tibble: 3 x 4\r\n##      hp   cyl  qsec `hp\/cyl`\r\n##   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;\r\n## 1   110     6  16.5     18.3\r\n## 2   110     6  17.0     18.3\r\n## 3    93     4  18.6     23.2\r\n<\/pre>\n<pre><code class=\"r\">tb2 %&gt;% mutate(hpPerCyl = hp\/cyl)\r\n<\/code><\/pre>\n<pre>## # A tibble: 3 x 4\r\n##      hp   cyl  qsec hpPerCyl\r\n##   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;\r\n## 1   110     6  16.5     18.3\r\n## 2   110     6  17.0     18.3\r\n## 3    93     4  18.6     23.2\r\n<\/pre>\n<pre><code class=\"r\">tb2 %&gt;% mutate(hpPerCyl = hp\/cyl, V2 = hp*qsec)\r\n<\/code><\/pre>\n<pre>## # A tibble: 3 x 5\r\n##      hp   cyl  qsec hpPerCyl    V2\r\n##   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt;\r\n## 1   110     6  16.5     18.3 1811.\r\n## 2   110     6  17.0     18.3 1872.\r\n## 3    93     4  18.6     23.2 1731.\r\n<\/pre>\n<ul>\n<li><code>tb2 %&gt;% mutate(hp\/cyl)<\/code>\ub97c \uacb0\uacfc\ub97c \ubcf4\uba74 \uc5f4\uc774\ub984\uc774 <code>hp\/cyl<\/code>\ub85c \ub418\uc5b4 \uc788\ub2e4. <code>hp\/cyl<\/code> \uacfc \uac19\uc740 \uc5f4\uc774\ub984\uc740 <code>hp \ub098\ub204\uae30 cyl<\/code> \uacfc \uad6c\ubd84\ud560 \uc218 \uc5c6\uae30 \ub54c\ubb38\uc5d0 \uc798 \uc4f0\uc774\uc9c0 \uc54a\ub294\ub2e4. \ud558\uc9c0\ub9cc <code>hp\/cyl<\/code>\uc744 \uc5f4\uc774\ub984\uc73c\ub85c \uc815\ud574\uc904 \uc218 \uc788\uc73c\uba70, \uc774\ub97c \uc0ac\uc6a9\ud558\ub824\uba74 \ub2e4\uc74c\uacfc \uac19\uc774 <code>\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc5f4\uc774\ub984\uc744 \uac10\uc2f8\uc900\ub2e4. (\ub2e4\uc74c \uc608\uc5d0\uc11c<\/code>mutate\uc758 \uacb0\uacfc\ub294 \ub530\ub85c \ubcc0\uc218\uc5d0 \ub2f4\uc544\uc57c \ud568\uc744 \uc8fc\uc758\ud558\uc790.)<\/li>\n<\/ul>\n<pre><code class=\"r\">tb2$`hp\/cyl` \r\n<\/code><\/pre>\n<pre>## Warning: Unknown or uninitialised column: 'hp\/cyl'.\r\n<\/pre>\n<pre>## NULL\r\n<\/pre>\n<pre><code class=\"r\">tb3 &lt;- tb2 %&gt;% mutate(hp\/cyl) \r\ntb3$`hp\/cyl` \r\n<\/code><\/pre>\n<pre>## [1] 18.33333 18.33333 23.25000\r\n<\/pre>\n<ul>\n<li>\ub2e4\uc74c\uacfc \uac19\uc740 \ub370\uc774\ud130\ud504\ub808\uc784\uc758 \uc804\ud1b5\uc801 \ubc29\ubc95\uacfc \ube44\uad50\ud574\ubcf4\uc790.<\/li>\n<\/ul>\n<pre><code class=\"r\">tb$V2 = with(tb, hp*qsec)\r\ntb[c('V1', 'V2')] = data.frame(tb$hp\/tb$cyl, tb$hp*tb$qsec)\r\n<\/code><\/pre>\n<h3>\uc815\ub82c\ud558\uae30<\/h3>\n<ul>\n<li>\ud328\ud0a4\uc9c0 <code>dplyr<\/code>\uc740 \ub370\uc774\ud130 \ud504\ub808\uc784\uc744 \uc815\ub82c\ud558\ub294 \uc9c1\uad00\uc801\uc778 \ubc29\ubc95\uc744 \uc81c\uacf5\ud55c\ub2e4. \ub9cc\uc57d <code>tb<\/code>\uc758 <code>cyl<\/code> \uc5f4\uc744 \uae30\uc900\uc73c\ub85c \uc815\ub82c\uc744 \ud558\uace0 \uc2f6\ub2e4\uba74, <code>tb %&gt;% arrange(cyl)<\/code>\ub77c\uace0 \uc4f4\ub2e4. \ub0b4\ub9bc\ucc28\uc21c \uc815\ub82c\uc740 <code>tb %&gt;% arrange(desc(cyl))<\/code>\ub77c\uace0 \uc4f4\ub2e4.<\/li>\n<li>\uae30\uc874\uc758 \ubc29\ubc95\uacfc \ube44\uad50\ud574 \ubcf4\uc790.<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th align=\"center\"><code>dplyr<\/code>\uc758 \ud568\uc218<\/th>\n<th align=\"center\">\uae30\uc874\uc758 \ubc29\ubc95<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\"><code>tb %&gt;% arrange(cyl)<\/code><\/td>\n<td align=\"center\"><code>tb[order(tb$cyl), ]<\/code><\/td>\n<\/tr>\n<tr>\n<td align=\"center\"><code>tb %&gt;% arrange(desc(cyl))<\/code><\/td>\n<td align=\"center\"><code>tb[order(tb$cyl, decreasing = T), ]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\ub9cc\uc57d <code>cyl<\/code>\uc758 \uc62c\ub9bc\ucc28\uc21c, <code>hp<\/code>\uc758 \ub0b4\ub9bc\ucc28\uc21c\uc73c\ub85c \uc815\ub82c\ud558\uace0 \uc2f6\ub2e4\uba74 <code>arrange()<\/code>\uc640 <code>desc()<\/code>\ub97c \uc0ac\uc6a9\ud558\uba74 \ub2e4\uc74c\uacfc \uac19\uc774 \uac04\ub2e8\ud558\uac8c \uc4f8 \uc218 \uc788\ub2e4. \uae30\uc874\uc758 \ubc29\ubc95\uc73c\ub85c\ub294 <code>tb[order(tb$cyl, -tb$qsec),]<\/code>\ub85c \uc4f8 \uc218 \uc788\ub2e4.[<sup>d2]<\/sup><\/li>\n<\/ul>\n<p>[<sup>d2]:\ub2e4\ub978<\/sup> \ubc29\ubc95\uc73c\ub85c\ub294 <code>tb[order(tb$cyl, tb$qsec, decreasing = c(F, T))]]<\/code>\ub85c \uc4f8 \uc218 \uc788\ub2e4. \ub9cc\uc57d <code>tb$cyl<\/code>\uc774\ub098 <code>tb$qsec<\/code>\uac00 \ubb38\uc790\uc5f4\uc774\uc5c8\ub2e4\uba74 \uc774 \ubc29\ubc95\uc744 \uc11c\uc57c \ud55c\ub2e4.<\/p>\n<pre><code class=\"r\">tb3 %&gt;% arrange(cyl, desc(qsec))\r\n<\/code><\/pre>\n<pre>## # A tibble: 3 x 4\r\n##      hp   cyl  qsec `hp\/cyl`\r\n##   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;\r\n## 1    93     4  18.6     23.2\r\n## 2   110     6  17.0     18.3\r\n## 3   110     6  16.5     18.3\r\n<\/pre>\n<h3>\uc694\uc57d\ud558\uae30<\/h3>\n<ul>\n<li><code>summarise<\/code> \ud568\uc218[<sup>d1]\ub294<\/sup> \ub370\uc774\ud130 \ud504\ub808\uc784\uc758 \ub0b4\uc6a9\uc744 \uba87 \uac1c\uc758 \uc694\uc57d\uac12\uc73c\ub85c \uc815\ub9ac\ud560 \uc218 \uc788\uac8c \ub3c4\uc640\uc900\ub2e4. \ub9cc\uc57d <code>tb<\/code>\uc758 <code>hp<\/code> \uc5f4\uc758 \ud3c9\uade0\uc744 \uad6c\ud558\uace0 \uc2f6\ub2e4\uba74 \ub2e4\uc74c\uc758 \ubc29\ubc95\uc744 \uc0ac\uc6a9\ud55c\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">tb %&gt;% summarise(mean(hp)) \r\n<\/code><\/pre>\n<pre>## # A tibble: 1 x 1\r\n##   `mean(hp)`\r\n##        &lt;dbl&gt;\r\n## 1       147.\r\n<\/pre>\n<pre><code class=\"r\">tb %&gt;% summarize(V1 = mean(hp))\r\n<\/code><\/pre>\n<pre>## # A tibble: 1 x 1\r\n##      V1\r\n##   &lt;dbl&gt;\r\n## 1  147.\r\n<\/pre>\n<pre><code class=\"r\">tb %&gt;% summarise(hpMean = mean(hp), qsecMedian =median(qsec))\r\n<\/code><\/pre>\n<pre>## # A tibble: 1 x 2\r\n##   hpMean qsecMedian\r\n##    &lt;dbl&gt;      &lt;dbl&gt;\r\n## 1   147.       17.7\r\n<\/pre>\n<p>[<sup>d1]:<\/sup> <code>summarize<\/code>\ub77c\uace0 \uc4f8 \uc218\ub3c4 \uc788\ub2e4<\/p>\n<ul>\n<li>\uacb0\uacfc\ub294 \ud2f0\ube14 \ud615\uc2dd\uc774\ub2e4.(<code># A Tibble: 1 x 1<\/code> \ub97c \uc8fc\ubaa9\ud558\uc790.) \uc0c8\ub85c\uc774 \uc0dd\uc131\ub41c \ud2f0\ube14\uc758 \uc5f4\uc774\ub984\uc740 \uc0c8\ub86d\uac8c \uc124\uc815\ud558\uc9c0 \uc54a\ub294\ub2e4\uba74 <code>summarsize( )<\/code> \uad04\ud638 \uc548\uc758 \uc218\uc2dd\uc774 \uadf8\ub300\ub85c \uc9c0\uc815\ub41c\ub2e4(\uc608. <code>mean(hp<\/code>). \uc5ec\ub7ec \uc5f4\uc744 \uc0ac\uc6a9\ud560 \uc218\ub3c4 \uc788\uace0, \uc5ec\ub7ec \uc5f4\uc744 \uc0dd\uc131\ud560 \uc218\ub3c4 \uc788\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">tb %&gt;% summarise(newVar1 = mean(hp) + median(qsec))\r\n<\/code><\/pre>\n<pre>## # A tibble: 1 x 1\r\n##   newVar1\r\n##     &lt;dbl&gt;\r\n## 1    164.\r\n<\/pre>\n<pre><code class=\"r\">tb %&gt;% summarise(newVar1 = mean(hp), newVar2 = median(qsec))\r\n<\/code><\/pre>\n<pre>## # A tibble: 1 x 2\r\n##   newVar1 newVar2\r\n##     &lt;dbl&gt;   &lt;dbl&gt;\r\n## 1    147.    17.7\r\n<\/pre>\n<ul>\n<li>\uadf8\ub9ac\uace0 \uc0c8\ub86d\uac8c \uc0dd\uc131\ub41c \uc5f4\uc740 \ubc14\ub85c \ub2e4\uc74c \uc5f4\uc5d0\uc11c \ud65c\uc6a9\ud560 \uc218\ub3c4 \uc788\ub2e4(<code>newVar3 = newVar1 + newVar2<\/code>). \uc774\ub294 \ub370\uc774\ud130 \ud504\ub808\uc784\uc5d0\ub294 \uc0ac\uc6a9\uc774 \ubd88\uac00\ub2a5\ud558\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">tb %&gt;% summarise(newVar1 = mean(hp), newVar2 = median(qsec), newVar3 = newVar1 + newVar2)\r\n<\/code><\/pre>\n<pre>## # A tibble: 1 x 3\r\n##   newVar1 newVar2 newVar3\r\n##     &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;\r\n## 1    147.    17.7    164.\r\n<\/pre>\n<pre><code class=\"r\">data.frame(mean(tb$hp), median(tb$qsec), newVar1+newVar2)\r\n<\/code><\/pre>\n<pre>## Error in data.frame(mean(tb$hp), median(tb$qsec), newVar1 + newVar2): object 'newVar1' not found\r\n<\/pre>\n<h3>\uc9d1\ub2e8\ubcc4\ub85c \ub098\ub204\uae30<\/h3>\n<ul>\n<li>\uc694\uc57d\ud558\ub294 \ud568\uc218 <code>summarise<\/code>\ub294 \uadf8 \uc790\uccb4\ub85c\ub3c4 \uc758\ubbf8\uac00 \uc788\uc9c0\ub9cc \uc9d1\ub2e8\uc744 \ub098\ub204\ub294 \ud568\uc218 <code>group_by<\/code>\uc640 \ud568\uaed8 \uc790\uc8fc \uc4f0\uc778\ub2e4. \ud568\uc218 <code>group_by<\/code>\ub294 \ud2b9\uc815\ud55c \uc5f4\uc758 \uac12\uc5d0 \ub530\ub77c \ud2f0\ube14(\ub610\ub294 \ub370\uc774\ud130 \ud504\ub808\uc784)\uc744 \ub098\ub204\ub294 \uc5ed\ud560\uc744 \ud55c\ub2e4. \ub2e4\uc74c\uc758 \uc608\ub97c \ubcf4\uc790.<\/li>\n<\/ul>\n<pre><code class=\"r\">tb3 %&gt;% group_by(cyl) \r\n<\/code><\/pre>\n<pre>## # A tibble: 3 x 4\r\n## # Groups:   cyl [2]\r\n##      hp   cyl  qsec `hp\/cyl`\r\n##   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;\r\n## 1   110     6  16.5     18.3\r\n## 2   110     6  17.0     18.3\r\n## 3    93     4  18.6     23.2\r\n<\/pre>\n<pre><code class=\"r\">tb3_grp &lt;- tb3 %&gt;% group_by(cyl) \r\nclass(tb3_grp) \r\n<\/code><\/pre>\n<pre>## [1] \"grouped_df\" \"tbl_df\"     \"tbl\"        \"data.frame\"\r\n<\/pre>\n<ul>\n<li><code>group_by<\/code>\uc758 \uacb0\uacfc\ub294 \uadf8\ub8f9\uc774 \ub098\ub220\uc9c4 \ud2f0\ube14\ub85c \ub370\uc774\ud130 \ud504\ub808\uc784\uacfc\ub3c4 \ud638\ud658\uc774 \ub41c\ub2e4(<code>group_by<\/code>\uc758 \uacb0\uacfc\uc5d0\uc11c <code># Groups: cyl [2]<\/code>\ub97c \uc8fc\ubaa9\ud558\uc790). \uc9d1\ub2e8\uc774 \ub098\ub220\uc9c4 \ud2f0\ube14\uc740 \ud2b9\ud788 <code>summarise<\/code> \ud568\uc218\uc640 <code>do<\/code> \ud568\uc218\uc640 \ud568\uaed8 \uc0ac\uc6a9\ub41c\ub2e4.<\/li>\n<\/ul>\n<h3>\uc9d1\ub2e8\ubcc4\ub85c \uc694\uc57d\ud558\uae30<\/h3>\n<ul>\n<li><code>group_by<\/code>\uc640 <code>summarise<\/code> \ud568\uc218\ub97c <code>%&gt;%<\/code>\ub85c \uc5f0\uacb0\ud558\uc5ec \uc8fc\uc5b4\uc9c4 \ud2f0\ube14\uc744 \uc9d1\ub2e8\ubcc4\ub85c \uc694\uc57d\ud560 \uc218 \uc788\ub2e4. \uc608\ub97c \ub4e4\uc5b4 <code>tb<\/code>(<code>mtcars<\/code>)\uc5d0\uc11c \uc790\ub3d9 \uae30\uc5b4(automatic)\uc640 \uc218\ub3d9 \uae30\uc5b4(manual)\ub85c \uc9d1\ub2e8\uc744 \ub098\ub208 \ub4a4, 0.25 \ub9c8\uc77c \uac00\uc18d \uc2dc\uac04(<code>qsec<\/code>)\uc758 \ud3c9\uade0\uc744 \uad6c\ud558\uace0\uc790 \ud55c\ub2e4\uba74 \ub2e4\uc74c\uc758 \uba85\ub839\uc744 \ud65c\uc6a9\ud560 \uc218 \uc788\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">tb %&gt;% group_by(am) %&gt;% summarise(mean(qsec))\r\n<\/code><\/pre>\n<pre>## # A tibble: 2 x 2\r\n##      am `mean(qsec)`\r\n##   &lt;dbl&gt;        &lt;dbl&gt;\r\n## 1     0         18.2\r\n## 2     1         17.4\r\n<\/pre>\n<ul>\n<li>\uadf8 \uacb0\uacfc\ub294 \uc704\uc5d0\uc11c \ud655\uc778\ud560 \uc218 \uc788\ub2e4. \uc218\ub3d9(<code>am<\/code> = 0)\uc77c \ub54c, 0.25 \ub9c8\uc77c \uac00\uc18d \uc2dc\uac04 \ud3c9\uade0\uc740 18.2\ucd08\uc774\uace0, \uc790\ub3d9(<code>am<\/code>=1)\uc77c \ub54c, 0.25 \ub9c8\uc77c \uac00\uc18d \uc2dc\uac04 \ud3c9\uade0\uc740 17.4\ucd08\uc784\uc744 \ud655\uc778\ud560 \uc218 \uc788\ub2e4.<\/li>\n<\/ul>\n<h3>\uc9d1\ub2e8\ubcc4\ub85c \ub098\ub208 \ud2f0\ube14\uc5d0 \ub300\ud574 \ud568\uc218 \uc801\uc6a9\ud558\uae30<\/h3>\n<ul>\n<li>\uc55e\uc5d0\uc11c \uc694\uc57d\uc744 \ud558\uae30 \uc704\ud574 \uc0ac\uc6a9\ud55c <code>summarise( )<\/code>\uc758 \uad04\ud638 \uc548\uc5d0 \uc4f0\uc774\ub294 \ud568\uc218\ub294 \uacb0\uacfc\ub85c \ud558\ub098\uc758 \uac12\uc744 \ubc18\ud658\ud574\uc57c \ud55c\ub2e4. \ub9cc\uc57d <code>range( )<\/code>\ucc98\ub7fc \ub450 \uac1c\uc758 \uac12 (\ucd5c\uc18c\uac12\uacfc \ucd5c\ub300\uac12)\uc744 \ubc18\ud658\ud558\ub294 \uacbd\uc6b0\uc5d0\ub294 \ub2e4\uc74c\uacfc \uac19\uc774 \uc5d0\ub7ec\uac00 \ubc1c\uc0dd\ud55c\ub2e4. \uadf8\ub9ac\uace0 <code>head<\/code>\ucc98\ub7fc \ub370\uc774\ud130 \ud504\ub808\uc784\uc744 \uc785\ub825\uc73c\ub85c \ubc1b\uc544 \ub370\uc774\ud130 \ud504\ub808\uc784\uc744 \ubc18\ud658\ud558\ub294 \ud568\uc218\ub3c4 \uc4f8 \uc218 \uc5c6\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">tb %&gt;% summarise(range(hp))\r\n<\/code><\/pre>\n<pre>## Error in summarise_impl(.data, dots): Column `range(hp)` must be length 1 (a summary value), not 2\r\n<\/pre>\n<ul>\n<li>\uc9d1\ub2e8\ubcc4\ub85c \ub098\ub220\uc9c4 \ud2f0\ube14(\ub370\uc774\ud130\ud14c\uc774\ube14)\uc5d0 \ub300\ud574 \ud568\uc218\ub97c \uc801\uc6a9\ud558\uc5ec \ub370\uc774\ud130\ud14c\uc774\ube14\uc774 \ubc18\ud658\ub418\ub294 \uacbd\uc6b0\uc5d0\ub294 <code>do( )<\/code> \ud568\uc218\ub97c \uc4f8 \uc218 \uc788\ub2e4. \ub2e4\uc74c\uc758 \uc608\ub97c \ubcf4\uc790.<\/li>\n<\/ul>\n<pre><code class=\"r\">tb %&gt;% group_by(am) %&gt;% do(head(., n=2))\r\n<\/code><\/pre>\n<pre>## # A tibble: 4 x 13\r\n## # Groups:   am [2]\r\n##     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb    V2\r\n##   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;\r\n## 1  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1 2138.\r\n## 2  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2 2978.\r\n## 3  21       6   160   110  3.9   2.62  16.5     0     1     4     4 1811.\r\n## 4  21       6   160   110  3.9   2.88  17.0     0     1     4     4 1872.\r\n## # ... with 1 more variable: V1 &lt;dbl&gt;\r\n<\/pre>\n<ul>\n<li>\uc704\uc758 \uc608\uc5d0\uc11c <code>tb<\/code>\ub294 \uc5f4 <code>am<\/code>\uc758 \uac12\uc5d0 \ub530\ub77c \ub450 \ub370\uc774\ud130 \ud504\ub808\uc784\uc744 \ub098\ub220\uc9c4 \ud6c4, \uac01 \ub370\uc774\ud130 \ud504\ub808\uc784\uc5d0 <code>head(., n=2)<\/code>\uac00 \uc801\uc6a9\ub41c\ub2e4. \uc774\ub807\uac8c \uc0dd\uc131\ub41c \ub450 \ub370\uc774\ud130 \ud504\ub808\uc784\uc774 \ud569\uccd0\uc838\uc11c \uacb0\uacfc \ub370\uc774\ud130 \ud504\ub808\uc784\uc774 \uc0dd\uc131\ub41c\ub2e4.<\/li>\n<li>\uc9d1\ub2e8 \ubcc4\ub85c \uc801\uc6a9\ub418\ub294 \ud568\uc218\ub294 \uacb0\uacfc\uac00 \ub370\uc774\ud130 \ud504\ub808\uc784\uc774\uc5b4\uc57c \ud568\uc744 \uc720\uc758\ud558\uc790. \uc608\ub97c \ub4e4\uc5b4 <code>summary<\/code> \ud568\uc218\ub294 \ub370\uc774\ud130 \ud504\ub808\uc784\uc744 \uc785\ub825\uc73c\ub85c \ubc1b\uc9c0\ub9cc \uacb0\uacfc\ub294 \ub370\uc774\ud130 \ud504\ub808\uc784\uc774 \uc544\ub2c8\ub2e4. \ud558\uc9c0\ub9cc <code>summary()<\/code> \uacb0\uacfc\ub294 <code>as.data.frame<\/code>\uc744 \ud65c\uc6a9\ud558\uc5ec \uc27d\uac8c \ub370\uc774\ud130 \ud504\ub808\uc784\uc73c\ub85c \ubc14\uafc0 \uc218 \uc788\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">tb %&gt;% group_by(am) %&gt;% do(summary(.))\r\n<\/code><\/pre>\n<pre>## Error: Results 1, 2 must be data frames, not table\r\n<\/pre>\n<pre><code class=\"r\">tb %&gt;% group_by(am) %&gt;% \r\n  do(as.data.frame(summary(.))) %&gt;% \r\n  slice(1:3)\r\n<\/code><\/pre>\n<pre>## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character\r\n<\/pre>\n<pre>## Warning in bind_rows_(x, .id): binding character and factor vector,\r\n## coercing into character vector\r\n\r\n## Warning in bind_rows_(x, .id): binding character and factor vector,\r\n## coercing into character vector\r\n<\/pre>\n<pre>## # A tibble: 6 x 4\r\n## # Groups:   am [2]\r\n##      am Var1  Var2       Freq             \r\n##   &lt;dbl&gt; &lt;fct&gt; &lt;fct&gt;      &lt;chr&gt;            \r\n## 1     0 \"\"    \"     mpg\" \"Min.   :10.40  \"\r\n## 2     0 \"\"    \"     mpg\" \"1st Qu.:14.95  \"\r\n## 3     0 \"\"    \"     mpg\" \"Median :17.30  \"\r\n## 4     1 \"\"    \"     mpg\" \"Min.   :15.00  \"\r\n## 5     1 \"\"    \"     mpg\" \"1st Qu.:21.00  \"\r\n## 6     1 \"\"    \"     mpg\" \"Median :22.80  \"\r\n<\/pre>\n<h3>\ud328\ud0a4\uc9c0 <code>dplyr<\/code>\uc744 \ud65c\uc6a9\ud558\uc5ec \ub370\uc774\ud130 \uac00\uacf5\ud558\uae30 \uc885\ud569<\/h3>\n<ul>\n<li>\uc55e\uc5d0\uc11c \uc18c\uac1c\ud55c \ud568\uc218\ub4e4\uc744 \uc0ac\uc6a9\ud558\uc5ec \uc8fc\uc5b4\uc9c4 \ub370\uc774\ud130 \ud504\ub808\uc784 tb\uc5d0\uc11c \ud544\uc694\ud55c \ubd80\ubd84\uc744 \uc120\ubcc4\ud558\uace0 \uc9d1\ub2e8\ubcc4\ub85c \ub098\ub208 \ud6c4 \uac00\uacf5\ud558\ub294 \uc808\ucc28\ub294 \ubcf4\ud1b5 \ub2e4\uc74c\uc758 \uc21c\uc11c\ub97c \ub530\ub978\ub2e4.<\/li>\n<\/ul>\n<p><code>tb %&gt;% select() %&gt;% filter() %&gt;% group_by() %&gt;%<\/code><\/p>\n<p><code>summarise()<\/code>, <code>do()<\/code>, <code>arrange(, .by_group=T)<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>dplyr\uc758 \ubc29\uc2dd : \uc218\uc815 \uc0c8\ub85c\uc6b4 \uc5f4 \ucd94\uac00 \uc0c8\ub85c\uc6b4 \uc5f4\uc744 \ucd94\uac00\ud558\uace0\uc790 \ud55c\ub2e4\uba74 mutate \ud568\uc218\ub97c \uc0ac\uc6a9\ud55c\ub2e4. \uc5f4\uc774\ub984\uc740 \uc815\ud558\uac70\ub098 \uc0dd\ub7b5\ud560 \uc218 \uc788\ub2e4. \uc5ec\ub7ec \uc5f4\uc744 \ud568\uaed8 \ucd94\uac00\ud560 \uc218\ub3c4 \uc788\ub2e4. library(dplyr) data(mtcars) tb = as_tibble(mtcars) tb2 &lt;- tb %&gt;% select(hp, cyl, qsec) %&gt;% slice(1:3) tb2 %&gt;% mutate(hp\/cyl) ## # A tibble: 3 x 4 ## hp cyl qsec `hp\/cyl` ## [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2491,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[78,28],"tags":[84,85,83,86],"jetpack_featured_media_url":"http:\/\/ds.sumeun.org\/wp-content\/uploads\/2019\/01\/gemstone-gef99953b8_640.jpg","_links":{"self":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/838"}],"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=838"}],"version-history":[{"count":3,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/838\/revisions"}],"predecessor-version":[{"id":2492,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/838\/revisions\/2492"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/media\/2491"}],"wp:attachment":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=838"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}