{"id":952,"date":"2019-02-28T10:48:09","date_gmt":"2019-02-28T01:48:09","guid":{"rendered":"http:\/\/141.164.34.82\/?p=952"},"modified":"2019-03-15T01:19:32","modified_gmt":"2019-03-14T16:19:32","slug":"%ed%9d%90%eb%a6%84%ec%a0%9c%ec%96%b41%ec%a1%b0%ea%b1%b4%ea%b3%bc-%eb%b0%98%eb%b3%b5","status":"publish","type":"post","link":"http:\/\/ds.sumeun.org\/?p=952","title":{"rendered":"\ud750\ub984\uc81c\uc5b4 01: \uc870\uac74\uacfc \ubc18\ubcf5"},"content":{"rendered":"<h1>\ud750\ub984 \uc81c\uc5b4<\/h1>\n<h3>\uc7ac\uc5f0 \uac00\ub2a5\uacfc \uc2a4\ud06c\ub9bd\ud2b8(Scripts)<\/h3>\n<ul>\n<li><strong>\uc2a4\ud06c\ub9bd\ud2b8<\/strong>\ub780 \uc21c\ucc28\uc801\uc73c\ub85c \uc2e4\ud589\ud558\uac8c \ub420 \ud568\uc218\ub4e4\uc744 \ubaa8\uc544 \ub193\uc740 \ud14d\uc2a4\ud2b8\uc774\ub2e4. \uc774\ub294 \ub450 \uac00\uc9c0 \uc774\uc720\uc5d0\uc11c \ud070 \uc758\ubbf8\uac00 \uc788\ub2e4.\n<ul>\n<li><strong>\ub3d9\uc77c\ud55c \ubd84\uc11d \uacfc\uc815\uc744 \uc7ac\uc5f0\ud560 \uc218 \uc788\ub2e4.<\/strong> \ub530\ub77c\uc11c \ub2e4\ub978 \uc0ac\ub78c\uc774 \uc27d\uac8c \ubd84\uc11d \uacfc\uc815\uc744 \uac80\uc99d\ud558\uace0 \uc218\uc815\ud560 \uc218 \uc788\ub2e4.<\/li>\n<li>\ub3d9\uc77c\ud55c \ubd84\uc11d\uc744 <strong>\ub2e4\ub978 \uc790\ub8cc<\/strong>\uc5d0 \uc801\uc6a9\ud560 \uc218 \uc788\ub2e4.<\/li>\n<\/ul>\n<\/li>\n<li>\ud2b9\ud788 \ucd5c\uadfc\uc5d0 <strong>\uc7ac\uc5f0\uac00\ub2a5\ud55c \uc5f0\uad6c<\/strong>\uac00 \ud070 \uc774\uc288\uac00 \ub418\uc5c8\ub2e4. \uadf8 \ud558\uc704 \uac1c\ub150\uc73c\ub85c <strong>\uc7ac\uc5f0 \uac00\ub2a5\ud55c \ubd84\uc11d<\/strong>\uc740 \ub204\uad6c\ub77c\ub3c4 \ub3d9\uc77c\ud55c \ub370\uc774\ud130\uc5d0 \ub3d9\uc77c\ud55c \ubd84\uc11d\uc744 \uc801\uc6a9\ud574\uc11c \ub3d9\uc77c\ud55c \ubd84\uc11d \uacb0\uacfc\ub97c \uc5bb\uc744 \uc218 \uc788\uc74c\uc744 \uc758\ubbf8\ud55c\ub2e4.<\/li>\n<li>R\uc5d0\uc11c \uc7ac\uc5f0 \uac00\ub2a5\ud55c \ubd84\uc11d\uc744 \ud558\uae30 \uc704\ud574\uc11c\ub294 \ub2e4\uc74c\uacfc \uac19\uc774 \uc124\uc815\ud558\ub294 \uac83\uc774 \uc88b\ub2e4.\n<ul>\n<li>\uc2a4\ud06c\ub9bd\ud2b8\ub97c \uc2e4\ud589\ud558\uae30 \uc804\uc5d0 <code>rm(list=ls())<\/code>\uc744 \uc2e4\ud589\ud558\uc5ec \uad00\ub828\uc5c6\ub294 \ubcc0\uc218\ub97c \ubaa8\ub450 \uc0ad\uc81c\ud55c\ub2e4.<\/li>\n<li>\ubd84\uc11d\uc744 \ud558\uae30 \uc804\uc5d0 \ud639\uc2dc \ub0a8\uc544 \uc788\uc744 \uc218 \uc788\ub294 \ubcc0\uc218\ub97c \ubaa8\ub450 \uc0ad\uc81c\ud558\ub294 \uac8c \uc88b\ub2e4. R Studio &#8211; Options &#8211; General &#8211; Restore .Rdata into workspace at startup\uc740 R Studio \uc2dc\uc791 \uc2dc <code>.RData<\/code>(\uc800\uc7a5\ud574\ub454 \ud658\uacbd)\uc744 \ubd88\ub7ec\ub4e4\uc5ec\uc624\ub294\uc9c0\ub97c \uc124\uc815\ud55c\ub2e4. \ud2b9\ubcc4\ud55c \uc774\uc720\uac00 \uc5c6\ub2e4\uba74 \uccb4\ud06c\ud558\uc9c0 \uc54a\ub294 \uac83\uc774 \uc88b\ub2e4.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\uc81c\uc5b4\ubb38: \uc870\uac74 \ub610\ub294 \ubc18\ubcf5<\/h2>\n<ul>\n<li>\uc77c\ub828\uc758 \ud568\uc218\ub4e4 \uc911 \uc77c\ubd80\ub97c \uc870\uac74\uc5d0 \ub530\ub77c, \ud639\uc740 \ubc18\ubcf5\uc801\uc73c\ub85c \uc2e4\ud589\ud574\uc57c \ud560 \uacbd\uc6b0\uac00<br \/>\n\uc788\ub2e4.<\/p>\n<ul>\n<li>\uc870\uac74\ubb38 : <code>if<\/code>\/<code>else<\/code>, <code>ifelse<\/code>, <code>switch<\/code><\/li>\n<li>\ubc18\ubcf5\ubb38 : <code>for<\/code>, <code>while<\/code>, <code>repeat<\/code>(<code>next<\/code>, <code>break<\/code>)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\uc870\uac74\ubb38<\/h3>\n<ul>\n<li>\uc870\uac74\uc5d0 \ub530\ub77c \ub2e4\ub978 \uacb0\uacfc\ub97c \uc5bb\uac8c \ub418\ub294 <code>if<\/code>\/<code>else<\/code>, <code>ifelse<\/code>, <code>switch<\/code>\uc911 <code>ifelse<\/code>\uc640 <code>switch<\/code>\ub294 \ud568\uc218\uc774\uace0, <code>if<\/code>\/<code>else<\/code>\ub294 \ud750\ub984 \uc81c\uc5b4\ubb38\uc774\ub2e4.\n<ul>\n<li><code>if<\/code>\/<code>else<\/code>\uc758 \uc758\ubbf8\ub294 \ubc14\ub014 \uc218 \uc5c6\uace0, <code>switch<\/code>\uc640 <code>ifelse<\/code>\ub294 \ub2e4\ub978 \ud568\uc218\ub85c \uc815\uc758\ud560 \uc218\ub3c4 \uc788\ub2e4.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"r\">s = 'here'\r\nif (s == \"here\") {\r\n  print(\"Seoul\")\r\n} else {\r\n  print(\"Somewhere else than Seoul\")\r\n}\r\n<\/code><\/pre>\n<pre>## [1] \"Seoul\"\r\n<\/pre>\n<pre><code class=\"r\">s2 &lt;- ifelse(s==\"here\",\r\n             \"Moon\", # if s==\"here\"\r\n             \"Somewhere else than Moon\") # if s!=\"here\"\r\n\r\nx &lt;- \"two\"\r\nswitch(x,\r\n       one = 1,\r\n       two = 2,\r\n       3)\r\n<\/code><\/pre>\n<pre>## [1] 2\r\n<\/pre>\n<pre><code class=\"r\">x &lt;- \"two\"; z &lt;- 3\r\nswitch(x,\r\n       one=1,\r\n       two={\r\n         y &lt;- paste(x,\"is entered.\")\r\n         z + 1},\r\n       three=z^2)\r\n<\/code><\/pre>\n<pre>## [1] 4\r\n<\/pre>\n<ul>\n<li><strong>\ubb38\uc81c:<\/strong> \ubcc0\uc218 <code>x<\/code>\uc5d0 <code>1<\/code>,<code>2<\/code>,<code>3<\/code>,<code>4<\/code> \uc911\uc758 \ud55c \uc218\uac00 \uc800\uc7a5\ub418\uc5b4 \uc788\ub2e4. <code>1<\/code>\uc77c \ub54c\ub294 <code>\"one\"<\/code>, <code>2<\/code>\uc77c \ub54c\ub294 <code>\"two\"<\/code>, \uadf8 \uc678\uc5d0\ub294 <code>else!<\/code>\ub77c\uace0 \ucd9c\ub825\ud558\ub294 R \uc2a4\ud06c\ub9bd\ud2b8\ub97c \uc791\uc131\ud558\uc138\uc694.<\/li>\n<\/ul>\n<h3>\ubc18\ubcf5\ubb38<\/h3>\n<ul>\n<li>\ub2e4\uc74c\uc740 1\ubd80\ud130 10\uae4c\uc9c0 \ub354\ud558\ub294 \ud504\ub85c\uadf8\ub7a8\uc744 <code>for<\/code>, <code>while<\/code>, <code>repeat<\/code>\ub97c \uc0ac\uc6a9\ud558\uc5ec \uad6c\ud604\ud558\uc600\ub2e4. for, while, repeat\ub294 \uac70\uc758 \ubaa8\ub4e0 \ucef4\ud4e8\ud130 \uc5b8\uc5b4\uc5d0\uc11c \uc0ac\uc6a9\ud558\ub294 \ubc18\ubcf5\ubb38\uc774\uae30 \ub54c\ubb38\uc5d0 \uc27d\uac8c \uc774\ud574\ud560 \uc218 \uc788\uc744 \uac83\uc774\ub2e4. \ub2e8, <code>repeat<\/code>\uc758 \uacbd\uc6b0 \uc870\uac74 \ubc18\ubcf5\uc774 \uc544\ub2c8\ub77c \ubb34\uc870\uac74 \ubb34\ud55c \ubc18\ubcf5\ud558\ub294 \uc81c\uc5b4\ubb38\uc774\uae30 <code>break<\/code>\ub85c \ubc16\uc73c\ub85c \ube60\uc838 \ub098\uc62c \uc218 \uc788\uc5b4\uc57c \ud55c\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\"># for\r\ns=0\r\nfor (i in 1:10)\r\n  s = s + i\r\nprint(s)\r\n<\/code><\/pre>\n<pre>## [1] 55\r\n<\/pre>\n<pre><code class=\"r\"># while\r\ns=0; i=1\r\nwhile (i &lt;= 10) {\r\n  s=s+i;\r\n  i=i+1\r\n}\r\nprint(s)\r\n<\/code><\/pre>\n<pre>## [1] 55\r\n<\/pre>\n<pre><code class=\"r\"># repeat\r\ns=0; i=1\r\nrepeat {\r\n  s=s+i; i=i+1\r\n  if (i&gt;10) break\r\n}\r\nprint(s)\r\n<\/code><\/pre>\n<pre>## [1] 55\r\n<\/pre>\n<ul>\n<li><code>break<\/code>\ub294 <code>for<\/code>, <code>while<\/code>, <code>repeat<\/code>\ub97c \ub05d\ub0b4\ub77c\ub294 \uc758\ubbf8\uc774\uace0, <code>next<\/code>\ub294 \ubc18\ubcf5\ub418\ub294 \ubd80\ubd84 \uc911 \ub098\uba38\uc9c0\ub97c \uac74\ub108\ub6f0\uace0 \ubc18\ubcf5\ub418\ub294 \ubd80\ubd84\uc758 \ucc98\uc74c\uc73c\ub85c \ub418\ub3cc\uc544 \uac00\ub77c\ub294 \uc758\ubbf8\uc774\ub2e4.<\/li>\n<\/ul>\n<h3>\ub2e4\uc911 \ubc18\ubcf5\ubb38<\/h3>\n<ul>\n<li><code>for<\/code>\uc640 \uac19\uc740 \ubc18\ubcf5 \uc548\uc5d0 \ub2e4\uc2dc <code>for<\/code>\ub97c \uc368\uc11c \ub2e4\uc911 \ubc18\ubcf5\ubb38\uc744 \ub9cc\ub4e4 \uc218 \uc788\ub2e4. <strong>\ubc18\ubcf5 \uc548\uc758 \ubc18\ubcf5<\/strong>\uc774\ub2e4. \ub7ec\uc2dc\uc544\uc758 \ub9c8\ud2b8\ub8cc\uc2dc\uce74<sup>[1]<\/sup> \ub098 \uc601\ud654 \uc778\uc149\uc158(Inception)<sup>[2]<\/sup> \uc758 \uafc8 \uc18d\uc758 \uafc8\uacfc \ube44\uc2b7\ud558\ub2e4.<\/li>\n<li>\ub2e4\uc911 \ubc18\ubcf5\ubb38\uc5d0\uc11c \uae4c\ub2e4\ub85c\uc6b4 \ubb38\uc81c \ud558\ub098\ub294 \uc5b4\ub5bb\uac8c \uae4a\uc740 \uafc8 \uc18d\uc5d0\uc11c \uae68\uc5b4\ub098\ub294\uac00\uc774\ub2e4. \uc5ec\uae30\uc5d0\ub294 \ub450 \uac00\uc9c0 \uc120\ud0dd\uc9c0\uac00 \uc788\uc744 \uc218 \uc788\ub2e4.\n<ul>\n<li>\uc5ec\ub7ec \ubc88 <code>break<\/code>\ub97c \ubc18\ubcf5\ud55c\ub2e4.<sup>[3]<\/sup><\/li>\n<li>\ud568\uc218 \uc548\uc5d0\uc11c <code>return<\/code>\uc744 \uc4f4\ub2e4.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><sup>[1]<\/sup> : \uae4c\uace0 \uae4c\ub3c4 \uc0c8\ub85c\uc6b4 \uc778\ud615\uc774 \ub098\uc624\ub294 \ub7ec\uc2dc\uc544\uc758 \ubaa9\uac01 \uc778\ud615<br \/>\n<sup>[2]<\/sup> : \uafc8 \uc18d\uc5d0\uc11c \ub2e4\uc2dc \uafc8\uc744 \uafb8\uace0, \uadf8 \uafc8 \uc18d\uc5d0\uc11c \ub2e4\uc2dc \uafc8\uc744 \uafb8\ub294 \ub0b4\uc6a9\uc758 \uc601\ud654<br \/>\n<sup>[3]<\/sup> : \uba87\uba87 \ud504\ub85c\uadf8\ub798\uba38\ub4e4\uc740 \uc774\ub7f0 \uc774\uc720\ub85c \uad6c\uc870\uc801 \ud504\ub85c\uadf8\ub798\uba38\ub4e4\uc758 \uc8fc\uc7a5\uacfc \ub2ec\ub9ac <code>goto<\/code>\ubb38\uc744 \uc801\uc808\ud558\uac8c \uc0ac\uc6a9\ud560 \uc218 \uc788\uc5b4\uc57c \ud55c\ub2e4\uace0 \uc8fc\uc7a5\ud55c\ub2e4.(\uad6c\uc870\uc801 \ud504\ub85c\uadf8\ub798\ubc0d: <code>goto<\/code>\uc640 \uac19\uc774 \uba4b\ub300\ub85c \ubd84\uae30\ud558\ub294 \ud504\ub85c\uadf8\ub7a8\uc740 \uc2a4\ud30c\uac8c\ud2f0 \ud504\ub85c\uadf8\ub7a8(\uc2a4\ud30c\uac8c\ud2f0\ucc98\ub7fc \ub108\ubb34 \uc5bd\ud0a4\uace0 \uc124\ucf1c\uc11c \uc0ac\ub78c\uc774 \uc774\ud574\ud558\uae30 \ud798\ub4e0 \ud504\ub85c\uadf8\ub7a8)\uc774 \ub418\uae30 \ub54c\ubb38\uc5d0 <code>goto<\/code>\ubb38\uc744 \uae08\uc9c0\ud558\uace0 <code>for<\/code> \ub610\ub294 <code>while<\/code>\uacfc \uac19\uc740 \ubc18\ubcf5\ubb38, \uc870\uac74\ubb38\uc73c\ub85c \ub300\uccb4\ud558\ub294 \ud504\ub85c\uadf8\ub798\ubc0d \ubc29\ubc95)<\/p>\n<pre><code class=\"r\">for (i in 1:10) {\r\n  for (j in 1:10) {\r\n    for (z in 1:10) {\r\n      if (i+j+z == 15) break\r\n    }\r\n    if (i+j+z == 15) break\r\n  }\r\n  if (i+j+z == 15) break\r\n}\r\nprint(c(i,j,z))\r\n<\/code><\/pre>\n<pre>## [1]  1  4 10\r\n<\/pre>\n<pre><code class=\"r\">f = function() {\r\n  for (i in 1:10) {\r\n    for (j in 1:10) {\r\n      for (z in 1:10) {\r\n        if (i+j+z == 15) return(c(i,j,z)) \r\n      }\r\n    }\r\n  }\r\n}\r\nf()\r\n<\/code><\/pre>\n<pre>## [1]  1  4 10\r\n<\/pre>\n<h3>\ubc18\ubcf5\ubb38 \ub2e4\uc2dc \ubcf4\uae30<\/h3>\n<h4>\ubc18\ubcf5\ubb38\uc758 \uae30\ubcf8\uc801\uc778 \uad6c\uc870<\/h4>\n<ol>\n<li>\uacb0\uacfc\ub97c \uc800\uc7a5\ud560 \uc7a5\uc18c(\ubcc0\uc218)\ub97c \ub9c8\ub828\ud55c\ub2e4.<\/li>\n<li>\ud2b9\uc815\ud55c \uac12\uc744 \ub530\ub77c, \ud639\uc740 \uc8fc\uc5b4\uc9c4 \uc870\uac74\uc774 \ub9cc\uc871\ud560 \ub54c\uae4c\uc9c0 \ubc18\ubcf5\ud558\ub3c4\ub85d \ud55c\ub2e4.<\/li>\n<li>\ubc18\ubcf5\ub418\ub294 \ub0b4\uc6a9<\/li>\n<\/ol>\n<ul>\n<li>\uc608: 1\ubd80\ud130 10\uae4c\uc9c0 \ub354\ud558\uae30<\/li>\n<\/ul>\n<pre><code class=\"r\">s=0 # \uacb0\uacfc\ub97c \uc800\uc7a5\ud560 \ubcc0\uc218 s\r\nfor (i in 1:10) # \ubcc0\uc218 i\uac00 1\ubd80\ud13010\uae4c\uc9c0 \ubc18\ubcf5\ub41c\ub2e4.\r\n  s = s + i # \ubc18\ubcf5\ub418\uc5b4 \uc218\ud589\ub418\ub294 \uc791\uc5c5\r\nprint(s)\r\n<\/code><\/pre>\n<pre>## [1] 55\r\n<\/pre>\n<ul>\n<li>\uc608: \ubca1\ud130 <code>x<\/code>\uc758 \ubaa8\ub4e0 \uc6d0\uc18c\uc5d0 \ub300\ud574 \uc81c\uacf1\ud55c \uac12 \uad6c\ud558\uae30<\/li>\n<\/ul>\n<pre><code class=\"r\">x = c(1, 3, 5, 9, 15)\r\ns = rep(NA, 5) # \uacb0\uacfc\ub97c \uc800\uc7a5\ud560 \ubcc0\uc218 s\r\n#for (i in 1:5) # \ubcc0\uc218 \uac00i \ubca1\ud130 \uc758x \uae38\uc774\uc5d0 \ub9de\ucdb0 \ubcc0\ud55c\ub2e4.\r\n#for (i in 1:length(x))\r\nfor (i in seq_along(x))\r\n  s[i] = x[i]^2 # \ubc18\ubcf5\ub418\uc5b4 \uc218\ud589\ub418\ub294 \uc791\uc5c5\r\nprint(s)\r\n<\/code><\/pre>\n<pre>## [1]   1   9  25  81 225\r\n<\/pre>\n<ul>\n<li>\uc704\uc758 \ucf54\ub4dc\uc5d0\ub294 <code>for<\/code>\ubb38\uc744 \ub3cc\ub9ac\ub294 3 \uac00\uc9c0 \ubc29\ubc95\uc744 \uc8fc\uc11d\ubb38\uc5d0 \uc81c\uc2dc\ud558\uc600\ub2e4.\n<ul>\n<li><code>for (i in 1:5)<\/code> : \ub9cc\uc57d \ubca1\ud130\uc758 \uae38\uc774\ub97c \uc54c\uace0, \uadf8 \uae38\uc774\uac00 \ubcc0\ud558\uc9c0 \uc54a\ub294\ub2e4\uba74.<\/li>\n<li><code>for (i in 1:length(x))<\/code> : \ub9cc\uc57d \ubca1\ud130\uc758 \uae38\uc774\ub97c \uc0ac\uc804\uc5d0 \uc54c \uc218 \uc5c6\ub2e4\uba74. \uc774\ub54c \uc7a0\uc7ac\uc801 \ubb38\uc81c\uc758 \ud558\ub098\ub294 \ubca1\ud130 <code>x<\/code>\uc758 \uae38\uc774\uac00 0\uc77c \uc218\ub3c4 \uc788\ub2e4\ub294 \uc810\uc774\ub2e4. \ub9cc\uc57d \ubca1\ud130 <code>x<\/code>\uc758 \uae38\uc774\uac00 0\uc774\ub77c\uba74 <code>for (i in 1:0)<\/code>\uc774 \uc2e4\ud589\ub418\uace0 <code>x[1]<\/code>\uacfc <code>x[0]<\/code>\uc740 \ubaa8\ub450 <code>NULL<\/code>\uc774\ub2e4.<\/li>\n<li><code>for (i in seq_along(x))<\/code> : \uc704\uc758 \ub2e8\uc810 \ub54c\ubb38\uc5d0 Hadley(2017)\uc740 \uc774 \ubc29\ubc95\uc744 \uad8c\uc7a5\ud588\ub2e4. <code>seq_along(x)<\/code>\ub294 \ubca1\ud130 <code>x<\/code>\uc758 \uae38\uc774\uac00 0\ubcf4\ub2e4 \ud074 \ub54c\uc5d0\ub294 <code>1:length(x)<\/code>\uc640 \ub3d9\uc77c\ud558\ub2e4. \ub9cc\uc57d \ubca1\ud130 <code>x<\/code>\uc758 \uae38\uc774\uac00 0\uc774\ub77c\uba74 <code>for<\/code>\ubb38\uc740 \uc2e4\ud589\ub418\uc9c0 \uc54a\ub294\ub2e4. \ud558\uc9c0\ub9cc \ud544\uc790\uac00 \uc774 \ubc29\ubc95\uc744 \uc0ac\uc6a9\ud574 \ubcf8 \uacb0\uacfc \ub2e8\uc810\uc774 \uc804\ud600 \uc5c6\ub294 \uac83\uc740 \uc544\ub2c8\uc5c8\ub2e4. \ub9cc\uc57d \uacc4\uc18d \uc2a4\ud06c\ub9bd\ud2b8\ub97c \uc218\uc815, \uac1c\uc120\ud558\uace0 \ub514\ubc84\uae45\uc744 \ud558\uace0 \uc788\ub294 \uacbd\uc6b0\uc5d0\ub294 <code>for<\/code>\ubb38\uc744 <code>i<\/code>\uac00 <code>1<\/code>\uc774 \uc544\ub2c8\ub77c \uc911\uac04 \uc22b\uc790\ubd80\ud130 \ub3cc\ub824\ubd10\uc57c \ud558\ub294 \uacbd\uc6b0\uac00 \uc0dd\uae30\uac8c \ub9c8\ub828\uc774\uae30 \ub54c\ubb38\uc774\ub2e4. \uc774 \uacbd\uc6b0\uc5d0\ub294 \ub2e4\uc74c\uc758 \ud615\ud0dc\ub97c \uc0ac\uc6a9\ud558\uba74 \ub420 \uac83\uc774\ub2e4.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"r\">x = c(1, 3, 5, 9, 15)\r\ns = rep(NA, 5) \r\n\r\nif (length(x)&gt;0) { \r\n  for (i in 1:length(x)) { \r\n    s[i] = x[i]^2\r\n    }\r\n  }\r\n<\/code><\/pre>\n<h4>\ubc18\ubcf5\ubb38\uc758 \ub300\uccb4<\/h4>\n<ul>\n<li>\ub2e4\uc74c\uc758 \ucf54\ub4dc\ub294 <code>for<\/code> \ubb38\uc744 \ub300\uccb4\ud558\ub294 \ub450 \uac00\uc9c0 \ubc29\ubc95\uc744 \ubcf4\uc5ec\uc900\ub2e4.\n<ul>\n<li>\ubca1\ud130\ud654\ub41c \ud568\uc218 : \ubca1\ud130\ud654\ub41c \ud568\uc218\ub294 \ubca1\ud130\uc758 \ubaa8\ub4e0 \uc6d0\uc18c\uc5d0 \ud568\uc218\ub97c \uc801\uc6a9\ud55c\ub2e4.<\/li>\n<li><code>sapply<\/code> \ud568\uc218 : <code>sapply(x, func)<\/code>\ub294 <code>x[[1]]<\/code>\uc5d0 <code>func<\/code>\uc744 \uc801\uc6a9\ud55c \uacb0\uacfc\ub97c \uccab \ubc88\uc9f8 \uc6d0\uc18c\uc5d0, <code>x[[2]]<\/code>\uc5d0 <code>func<\/code>\uc744 \uc801\uc6a9\ud55c \uacb0\uacfc\ub97c \ub450 \ubc88\uc9f8 \uc6d0\uc18c\uc5d0 \uc800\uc7a5\ud55c\ub2e4.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"r\">#01. Vectorized function\r\nx &lt;- c(1, 3, 5, 9, 15)\r\ns &lt;- sqrt(x); s\r\n<\/code><\/pre>\n<pre>## [1] 1.000000 1.732051 2.236068 3.000000 3.872983\r\n<\/pre>\n<pre><code class=\"r\">#02. sapply\r\nx &lt;- c(1, 3, 5, 9, 15)\r\ns &lt;- sapply(x, sqrt); s\r\n<\/code><\/pre>\n<pre>## [1] 1.000000 1.732051 2.236068 3.000000 3.872983\r\n<\/pre>\n<ul>\n<li>\ub2e4\uc74c\uc758 \uc608\ub294 \ubca1\ud130\ud654\ub418\uc9c0 \uc54a\uc740 \ud568\uc218\ub97c <code>Vectorize<\/code>\ub780 \ud568\uc218\ub97c \ud65c\uc6a9\ud558\uc5ec \ubca1\ud130\ud654\ud558\ub294 \uacfc\uc815\uc744 \ubcf4\uc5ec\uc900\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">#01. if the function is not vectorized\r\ntonum = function(x) {\r\n  switch(x,\r\n         one = 1,\r\n         two = 2,\r\n         3)\r\n}\r\nx &lt;- c(\"one\",\"three\",\"two\",\"four\",\"two\")\r\ns &lt;- tonum(x)\r\n<\/code><\/pre>\n<pre>## Error in switch(x, one = 1, two = 2, 3): EXPR must be a length 1 vector\r\n<\/pre>\n<pre><code class=\"r\">tonumV = Vectorize(tonum)\r\ns &lt;- tonumV(x)\r\nprint(s)\r\n<\/code><\/pre>\n<pre>##   one three   two  four   two \r\n##     1     3     2     3     2\r\n<\/pre>\n<pre><code class=\"r\">#02. sapply with Vectorized function\r\nx &lt;- c(\"one\",\"three\",\"two\",\"four\",\"two\")\r\ns &lt;- sapply(x, tonumV)\r\nprint(s)\r\n<\/code><\/pre>\n<pre>##     one.one three.three     two.two   four.four     two.two \r\n##           1           3           2           3           2\r\n<\/pre>\n<ul>\n<li><code>sapply<\/code>\uc744 <code>for<\/code>\ubb38\uc744 \ub300\uc2e0\ud574\uc11c \uc4f8 \uc218 \uc788\ub2e4. \uc608\uc804\uc5d0 \uc791\uc131\ub41c R \uc2a4\ud06c\ub9bd\ud2b8\uc5d0 \uc885\uc885 \ub4f1\uc7a5\ud55c\ub2e4. \ud558\uc9c0\ub9cc <code>for<\/code>\ubb38\uacfc \ub2ec\ub9ac <code>next<\/code>\/<code>break<\/code>\ub97c \uc0ac\uc6a9\ud560 \uc218 \uc5c6\uace0, \ucd5c\uadfc\uc5d0\ub294 \uc18d\ub3c4\ub3c4 \ub354 \ub290\ub9ac\uae30 \ub54c\ubb38\uc5d0 \ub2e4\ub978 \uc5b8\uc5b4 \uc0ac\uc6a9\uc790\ub4e4\uc744 \uace8\ud0d5\uba39\uc77c \uc758\ubbf8\uac00 \uc544\ub2c8\ub77c\uba74 \uad73\uc774 \uc0ac\uc6a9\ud560 \uc774\uc720\uac00 \uc5c6\uc5b4 \ubcf4\uc778\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\"># 01a. for\r\nx &lt;- c(1,3,7,2,5)\r\ns &lt;- rep(NA, length(x))\r\nfor (i in 1:10) {\r\n  s[i] &lt;- x[i]^2\r\n}\r\n\r\n# 01b. sapply\r\nx &lt;- c(1,3,7,2,5)\r\ns &lt;- rep(NA, length(x))\r\ns &lt;- sapply(1:5, function(i) x[i]^2)\r\n\r\n# 02a. for\r\nx &lt;- c(1,3,7,2,5)\r\ns &lt;- 0\r\nfor (i in 1:10) {\r\n  s &lt;- s + x[i]^2\r\n}\r\n\r\n# 02b. sapply\r\nx &lt;- c(1,3,7,2,5)\r\ns &lt;- 0\r\ninvisible(sapply(1:5, function(i) s &lt;&lt;- x[i]^2))\r\n<\/code><\/pre>\n<h4><code>for<\/code>\uc758 \uba87 \uac00\uc9c0 \ubcc0\ud615<\/h4>\n<ul>\n<li><code>for (x in xs), for (nm in names(xs))<\/code>\n<ul>\n<li><code>for<\/code> \ubb38\uc740 <code>for (i in 1:100)<\/code>\ucc98\ub7fc \uc778\ub371\uc2a4\ub97c \ud558\ub098\uc529 \uc99d\uac00\uc2dc\ud0a4\uc9c0 \uc54a\uace0, \ud544\uc694\ud55c \ub0b4\uc6a9\uc744 \ubca1\ud130 \ub610\ub294 \ub9ac\uc2a4\ud2b8\uc5d0 \uc800\uc7a5\ud558\uc5ec\uc11c \ubc18\ubcf5\ud558\uac70\ub098, \ud544\uc694\ud55c \ub0b4\uc6a9\uc744 \ub2f4\uace0 \uc788\ub294 \ubca1\ud130 \ub610\ub294 \ub9ac\uc2a4\ud2b8\uc758 \uc6d0\uc18c \uc774\ub984\uc744 \uc0ac\uc6a9\ud558\uc5ec \ubc18\ubcf5\ud560 \uc218\ub3c4 \uc788\ub2e4.<\/li>\n<\/ul>\n<\/li>\n<li>\uacb0\uacfc\uc758 \uae38\uc774\uac00 \uac00\ubcc0\uc801\uc77c \ub54c : \uacb0\uacfc\ub97c \uba3c\uc800 \ub9ac\uc2a4\ud2b8\uc5d0 \ub2f4\ub294\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">x &lt;- c(1,3,2,4)\r\nresult = vector(\"list\", length(x))\r\n#result = c()\r\nfor (i in seq_along(x)) {\r\n  result[[i]] = rep(x,x)\r\n  #result = c(result, rep(x,x))\r\n}\r\nresult &lt;- unlist(result)\r\n<\/code><\/pre>\n<ul>\n<li>\uc704\uc758 \ucf54\ub4dc\uc5d0\uc11c \uc8fc\uc11d\uc740 \ucd08\ubcf4\uc790\uac00 \ud558\uae30 \uc26c\uc6b4 \uc2e4\uc218\ub97c \ubcf4\uc5ec\uc8fc\uace0 \uc788\ub2e4. \ucc98\uc74c\uc5d0 \ube48 \ubca1\ud130\ub97c \ud558\ub098 \ub9cc\ub4e0 \ud6c4\uc5d0 \uacc4\uc18d \uae30\uc874\uc758 \ubca1\ud130\uc640 \uc0c8\ub85c\uc6b4 \ubca1\ud130\ub97c <strong>c<\/strong>oncatenate(\uc5f0\uacb0) \ud574 \ub098\uac04\ub2e4(<code>result=c(result, rep(x,x))<\/code>). \ud558\uc9c0\ub9cc \uc774\ub7f0 \ubc29\ubc95\uc740 \ubca1\ud130\uac00 \uae38\uc5b4\uc9c8 \uc218\ub85d \uc5c4\uccad\ub098\uac8c \ub290\ub824\uc9c0\ub294 \ub2e8\uc810\uc774 \uc788\uc73c\ubbc0\ub85c \uc9c0\uc591\ud574\uc57c \ud55c\ub2e4.<\/li>\n<li>\ubc18\ubcf5\uc758 \ud69f\uc218\uac00 \uac00\ubcc0\uc801\uc77c \ub54c : <code>while<\/code>, <code>repeat\/break<\/code>\n<ul>\n<li>\ub9cc\uc57d \ubc18\ubcf5\uc758 \ud69f\uc218\uac00 <code>for<\/code>\uc744 \uc2dc\uc791\ud558\uae30 \uc804\uc5d0 \uc54c \uc218 \uc5c6\uc744 \ub54c\uc5d0\ub294 <code>while<\/code> \ub610\ub294 <code>repeat<\/code> \ubb38\uc744 \uc368\uc57c \ud55c\ub2e4.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>\ubc18\ubcf5\ubb38\uc758 \uc18d\ub3c4 \ube44\uad50<\/h4>\n<ul>\n<li>\ub3d9\uc77c\ud55c \ubc18\ubcf5 \uc791\uc5c5\uc744 \uc218\ud589\ud558\ub294 \uc5ec\ub7ec \uac00\uc9c0 \ub2e4\ub978 \ubc29\ubc95\uc758 \uc18d\ub3c4\ub97c \ube44\uad50\ud574 \ubcf4\uba74 \uc18d\ub3c4\uc758 \uc21c\uc11c\ub294 \ub300\ubd80\ubd84 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/li>\n<\/ul>\n<blockquote><p>R\uc758 \ub0b4\uc7a5 \ud568\uc218 &gt; <code>apply<\/code> &gt; <code>for<\/code><\/p><\/blockquote>\n<pre><code class=\"r\">mat &lt;- matrix(1:1000, 1000, 1000, byrow=T)\r\n# R \ub0b4\uc7a5 \ud568\uc218\r\nresult &lt;- colSums(mat)\r\n\r\n# apply\r\nresult &lt;- apply(mat, 2, sum)\r\n\r\n# for\r\nresult &lt;- rep(NA, 1000) # result &lt;- NA, length(result) &lt;- 1000\r\nfor (i in 1:ncol(mat)) {\r\n  result[i] &lt;- sum(mat[,i])\r\n}\r\n<\/code><\/pre>\n<ul>\n<li>R\uc758 \uacfc\uac70 \ubc84\uc804\uc5d0\uc11c\ub294 <code>for<\/code>\ubb38\uc744 \uc4f0\ub294 \uac83\ubcf4\ub2e4 <code>apply<\/code>\uc744 \uc4f0\ub294 \uac83\uc774 \uc555\ub3c4\uc801\uc73c\ub85c \ube68\ub790\ub2e4. \ud558\uc9c0\ub9cc Hadley\uac00 \uc9c0\uc801\ud588\ub4ef\uc774 \ucd5c\uadfc \ubc84\uc804\uc758 R\uc5d0\uc11c\ub294 <code>for<\/code>\ubb38\uc744 \uc0ac\uc6a9\ud574\ub3c4 \uc131\ub2a5 \uc800\ud558\uac00 \uadf8\ub9ac \ud06c\uc9c0 \uc54a\ub2e4.<\/li>\n<li>\uc5ec\uae30\uc11c \uc7a0\uae50 \uc2e4\ud5d8\uc744 \ud574\ubcf4\uc790. \uc544\ub798\uc758 \ucf54\ub4dc\ub85c \ub3d9\uc77c\ud55c \uae30\ub2a5(\ud589\ub82c\uc758 \uac01 \uc5f4\uc744 \ud569\ud55c\ub2e4)\uc744 \ud558\ub294 \uc11c\ub85c \ub2e4\ub978 \ubc29\ubc95\uc5d0 \ub300\ud55c \uc18c\uc694 \uc2dc\uac04\uc744 \uce21\uc815\ud574 \ubcf4\uba74 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/li>\n<\/ul>\n<pre><code class=\"r\">#gc()\r\nmat &lt;- matrix(1:1000, 1000, 1000, byrow=T)\r\nresult &lt;- vector(mode=\"list\", 1000)\r\n# R \ub0b4\uc7a5 \ud568\uc218\r\ntColsum &lt;- system.time({for (i in 1:1000)\r\n  result[[i]] &lt;- colSums(mat)})\r\n\r\n#gc()\r\nmat &lt;- matrix(1:1000, 1000, 1000, byrow=T)\r\nresult &lt;- vector(mode=\"list\", 1000)\r\n# for\r\ntFor &lt;- system.time(for(iter in 1:1000) {\r\n  result[[iter]] &lt;- rep(NA, 1000) # result &lt;- NA, length(result) &lt;- 1000\r\n  for (i in 1:ncol(mat)) {\r\n    result[[iter]][i] &lt;- sum(mat[,i]) \r\n  }})\r\n\r\n#gc()\r\nmat &lt;- matrix(1:1000, 1000, 1000, byrow=T)\r\nresult &lt;- vector(mode=\"list\", 1000)\r\n# apply\r\ntApply &lt;- system.time({for (i in 1:1000)\r\n  result[[i]] &lt;- apply(mat, 2, sum)\r\n})\r\n<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th align=\"center\"><\/th>\n<th align=\"center\"><code>colSums<\/code><\/th>\n<th align=\"center\"><code>apply(  , 2, sum)<\/code><\/th>\n<th align=\"center\"><code>for<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">\uc18c\uc694 \uc2dc\uac04(\ucd08)<\/td>\n<td align=\"center\">0.86<\/td>\n<td align=\"center\">10.23<\/td>\n<td align=\"center\">5.45<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\uc704\uc758 \uacb0\uacfc\ub97c \ubcf4\uba74 \uc18d\ub3c4 \uc21c\uc11c\ub294 \uccab \ubc88\uc9f8\uac00 R\uc5d0\uc11c \uadf8 \uae30\ub2a5\ub9cc\uc744 \uc704\ud574 \ud2b9\ubcc4\ud788 \uc81c\uc791\ub41c \ud568\uc218, \ub450 \ubc88\uc9f8\uac00 <code>for<\/code>, \uc138 \ubc88\uc9f8\uac00 <code>apply<\/code>\uc774\ub2e4. \uc774 \uacbd\uc6b0\uc5d0\ub294 \uc704\uc5d0\uc11c \uc598\uae30\ud55c \uc77c\ubc18\uc801\uc778 \uacbd\uc6b0\uc640 \ub2e4\uc18c \ud070 \ucc28\uc774\ub97c \ubcf4\uc778\ub2e4. \ub530\ub77c\uc11c \uc18d\ub3c4\uac00 \uc911\uc694\ud55c \uacbd\uc6b0\uc5d0\ub294 \uc0c1\ud669\uacfc \uc870\uac74\uc5d0 \ub9de\ucdb0 \ubaa8\uc758 \uc2e4\ud5d8\uc744 \ud574\ubcf4\ub294 \uac83\uc774 \uc88b\uc744 \uac83\uc774\ub2e4.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\ud750\ub984 \uc81c\uc5b4 \uc7ac\uc5f0 \uac00\ub2a5\uacfc \uc2a4\ud06c\ub9bd\ud2b8(Scripts) \uc2a4\ud06c\ub9bd\ud2b8\ub780 \uc21c\ucc28\uc801\uc73c\ub85c \uc2e4\ud589\ud558\uac8c \ub420 \ud568\uc218\ub4e4\uc744 \ubaa8\uc544 \ub193\uc740 \ud14d\uc2a4\ud2b8\uc774\ub2e4. \uc774\ub294 \ub450 \uac00\uc9c0 \uc774\uc720\uc5d0\uc11c \ud070 \uc758\ubbf8\uac00 \uc788\ub2e4. \ub3d9\uc77c\ud55c \ubd84\uc11d \uacfc\uc815\uc744 \uc7ac\uc5f0\ud560 \uc218 \uc788\ub2e4. \ub530\ub77c\uc11c \ub2e4\ub978 \uc0ac\ub78c\uc774 \uc27d\uac8c \ubd84\uc11d \uacfc\uc815\uc744 \uac80\uc99d\ud558\uace0 \uc218\uc815\ud560 \uc218 \uc788\ub2e4. \ub3d9\uc77c\ud55c \ubd84\uc11d\uc744 \ub2e4\ub978 \uc790\ub8cc\uc5d0 \uc801\uc6a9\ud560 \uc218 \uc788\ub2e4. \ud2b9\ud788 \ucd5c\uadfc\uc5d0 \uc7ac\uc5f0\uac00\ub2a5\ud55c \uc5f0\uad6c\uac00 \ud070 \uc774\uc288\uac00 \ub418\uc5c8\ub2e4. \uadf8 \ud558\uc704 \uac1c\ub150\uc73c\ub85c \uc7ac\uc5f0 \uac00\ub2a5\ud55c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1033,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[178,28,151],"tags":[43,39,42,44,41,40],"jetpack_featured_media_url":"http:\/\/ds.sumeun.org\/wp-content\/uploads\/2019\/02\/640px-Russian-Matroshka.jpg","_links":{"self":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/952"}],"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=952"}],"version-history":[{"count":8,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/952\/revisions"}],"predecessor-version":[{"id":1034,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/952\/revisions\/1034"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/media\/1033"}],"wp:attachment":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=952"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}