{"id":275726,"date":"2016-03-09T12:38:02","date_gmt":"2016-03-09T09:38:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=275726"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2016-03-09T09:38:02","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=275726","title":{"rendered":"\u0421\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u043f\u043e \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044e \u043a\u043e\u0434\u0430 \u043d\u0430 R, \u0447\u0430\u0441\u0442\u044c 2"},"content":{"rendered":"<p>       \u0426\u0438\u043a\u043b for \u0432 R \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u043c, \u0435\u0441\u043b\u0438 \u043e\u043d \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435, \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u043c\u0435\u0442\u044c \u0434\u0435\u043b\u043e \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u043d\u0430\u0431\u043e\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u0442\u044c \u0440\u044f\u0434 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0430\u0448 \u043a\u043e\u0434 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0438 \u0432\u044b, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u0431\u0443\u0434\u0435\u0442\u0435 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u044b, \u0443\u0437\u043d\u0430\u0432 \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e.<\/p>\n<p>  \u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u043b\u043e\u0433\u0438\u043a\u0435, \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0438 <code>Rcpp<\/code>, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c 100 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u043e\u0432 \u0441\u0442\u0440\u043e\u043a \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u0431\u043e\u043b\u044c\u0448\u0435. <\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043a\u043e\u0434 \u0441 \u0446\u0438\u043a\u043b\u043e\u043c for \u0438 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c (if-else) \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u043e\u043d\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a \u043d\u0430\u0431\u043e\u0440\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 (data frame, df). \u041a\u043e\u0434 \u043d\u0438\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445.   <\/p>\n<pre><code class=\"python\"># \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 col1 &lt;- runif (12^5, 0, 2) col2 &lt;- rnorm (12^5, 0, 2) col3 &lt;- rpois (12^5, 3) col4 &lt;- rchisq (12^5, 2) df &lt;- data.frame (col1, col2, col3, col4) <\/code><\/pre>\n<p>  \u0412 <a href=\"https:\/\/habrahabr.ru\/company\/infopulse\/blog\/277681\/\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438<\/a>: \u0432\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0441\u0442\u0438\u043d\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f, ifelse.<br \/>  \u0412 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438: which, apply, \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u0430\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f, Rcpp, data.table, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.<br \/>  <a name=\"habracut\"><\/a><\/p>\n<h4>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 which()<\/h4>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0443 <code>which()<\/code> \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u0441\u0442\u0440\u043e\u043a, \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0441\u0442\u0438\u0447\u044c \u043e\u0434\u043d\u043e\u0439 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <code>Rcpp<\/code>.  <\/p>\n<pre><code class=\"python\"># \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0413\u0435\u0439\u0431 \u0411\u0435\u043a\u0435\u0440 system.time({   want = which(rowSums(df) &gt; 4)   output = rep(&quot;less than 4&quot;, times = nrow(df))   output[want] = &quot;greater than 4&quot; })  <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"diff\"># \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a = 3 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 (\u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e)    user  system elapsed    0.396   0.074   0.481  <\/code><\/pre>\n<h4>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u0435\u043c\u0435\u0439\u0441\u0442\u0432\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0439 apply \u0432\u043c\u0435\u0441\u0442\u043e \u0446\u0438\u043a\u043b\u043e\u0432 for<\/h4>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>apply()<\/code> \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u0442\u043e\u0439 \u0436\u0435 \u043b\u043e\u0433\u0438\u043a\u0438 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0441 \u0432\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c for. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0440\u0430\u0441\u0442\u0443\u0442 \u0441 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432, \u043d\u043e \u043e\u043d\u0438 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0447\u0435\u043c <code>ifelse()<\/code> \u0438 \u0432\u0435\u0440\u0441\u0438\u0438, \u0433\u0434\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u0435\u043b\u0430\u043b\u0430\u0441\u044c \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u0446\u0438\u043a\u043b\u0430. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e, \u043d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u0430\u044f \u0438\u0437\u043e\u0431\u0440\u0435\u0442\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438.   <\/p>\n<pre><code class=\"python\"># \u0441\u0435\u043c\u0435\u0439\u0441\u0442\u0432\u043e apply system.time({   myfunc &lt;- function(x) {     if ((x['col1'] + x['col2'] + x['col3'] + x['col4']) &gt; 4) {       &quot;greater_than_4&quot;     } else {       &quot;lesser_than_4&quot;     }   }   output &lt;- apply(df[, c(1:4)], 1, FUN=myfunc)  # \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c 'myfunc' \u043a \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435   df$output &lt;- output }) <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/edc\/270\/093\/edc2700934804905bd0338d2fdee7d7e.png\" width=\"60%\"\/><br \/>  <i>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 apply \u0438 \u0446\u0438\u043a\u043b\u0430 for \u0432 R<\/i><\/p>\n<h4>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439 cmpfun() \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 compiler \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/h4>\n<p>  \u042d\u0442\u043e, \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u043d\u0435 \u043b\u0443\u0447\u0448\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435, \u0447\u0435\u043c \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0444\u043e\u0440\u043c\u0430. \u041e\u0434\u043d\u0430\u043a\u043e, \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u0430\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f \u0434\u043e\u043a\u0430\u0437\u0430\u043b\u0430 \u0441\u0432\u043e\u044e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c. \u0414\u0443\u043c\u0430\u044e, \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u0441\u043b\u0443\u0447\u0430\u0435.  <\/p>\n<pre><code class=\"python\"># \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u0430\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f \u043a\u043e\u0434\u0430 library(compiler) myFuncCmp &lt;- cmpfun(myfunc) system.time({   output &lt;- apply(df[, c (1:4)], 1, FUN=myFuncCmp) }) <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/aab\/d80\/2df\/aabd802df9a24e85a6b3ef78140560ee.png\" width=\"60%\"\/><br \/>  <i>Apply, \u0446\u0438\u043a\u043b for \u0438 \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u0430\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f \u043a\u043e\u0434\u0430<\/i><\/p>\n<h4>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 Rcpp<\/h4>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u044b\u0439\u0434\u0435\u043c \u043d\u0430 \u043d\u043e\u0432\u044b\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c. \u0414\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0439 \u0438 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b\u0438, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>ifelse()<\/code> \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e. \u0427\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043d\u043e\u043b\u044c? \u041d\u0438\u0436\u0435 \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u044d\u0442\u0443 \u0436\u0435 \u043b\u043e\u0433\u0438\u043a\u0443 \u0441 <code>Rcpp<\/code>, \u0441 \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 100 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u043e\u0432 \u0441\u0442\u0440\u043e\u043a. \u041c\u044b \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <code>Rcpp<\/code> \u0438 <code>ifelse()<\/code>.  <\/p>\n<pre><code class=\"python\">library(Rcpp) sourceCpp(&quot;MyFunc.cpp&quot;) system.time (output &lt;- myFunc(df)) # \u0444\u0443\u043d\u043a\u0446\u0438\u044f Rcpp \u043d\u0438\u0436\u0435 <\/code><\/pre>\n<p>  \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0430 \u0442\u0430 \u0436\u0435 \u043b\u043e\u0433\u0438\u043a\u0430, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043d\u0430 \u0421++ \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u043a\u0435\u0442\u0430 Rcpp. \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u043a\u043e\u0434 \u043d\u0438\u0436\u0435 \u043a\u0430\u043a \u00abMyFunc.cpp\u00bb \u0432 \u0432\u0430\u0448\u0435\u0439 \u0440\u0430\u0431\u043e\u0447\u0435\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u0441\u0435\u0441\u0441\u0438\u0438 R (\u0438\u043b\u0438 \u0432\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c sourceCpp, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0443\u0442\u044c). \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 <code>\/\/ [[Rcpp::export]]<\/code> \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d, \u0438 \u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0437 R.  <\/p>\n<pre><code class=\"cpp\">\/\/ \u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0434\u043b\u044f MyFunc.cpp #include  using namespace Rcpp; \/\/ [[Rcpp::export]] CharacterVector myFunc(DataFrame x) {   NumericVector col1 = as(x[&quot;col1&quot;]);   NumericVector col2 = as(x[&quot;col2&quot;]);   NumericVector col3 = as(x[&quot;col3&quot;]);   NumericVector col4 = as(x[&quot;col4&quot;]);   int n = col1.size();   CharacterVector out(n);   for (int i=0; i 4){       out[i] = &quot;greater_than_4&quot;;     } else {       out[i] = &quot;lesser_than_4&quot;;     }   }   return out; } <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/75a\/07e\/430\/75a07e430be64d11a3da34d864707581.png\" width=\"60%\"\/><br \/>  <i>\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c <code>Rcpp<\/code> \u0438 <code>ifelse<\/code><\/i><\/p>\n<h4>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443, \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u043c\u043d\u043e\u0433\u043e\u044f\u0434\u0435\u0440\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440<\/h4>\n<p>  \u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430:  <\/p>\n<pre><code class=\"python\"># \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 library(foreach) library(doSNOW) cl &lt;- makeCluster(4, type=&quot;SOCK&quot;) # for 4 cores machine registerDoSNOW (cl) condition &lt;- (df$col1 + df$col2 + df$col3 + df$col4) &gt; 4 # \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u0432\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439 system.time({   output &lt;- foreach(i = 1:nrow(df), .combine=c) %dopar% {     if (condition[i]) {       return(&quot;greater_than_4&quot;)     } else {       return(&quot;lesser_than_4&quot;)     }   } })  df$output &lt;- output <\/code><\/pre>\n<h4>\u0423\u0434\u0430\u043b\u044f\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0447\u0438\u0449\u0430\u0439\u0442\u0435 \u043f\u0430\u043c\u044f\u0442\u044c \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u043d\u044c\u0448\u0435<\/h4>\n<p>  \u0423\u0434\u0430\u043b\u044f\u0439\u0442\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u043a\u043e\u0434\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>rm()<\/code>, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u043d\u044c\u0448\u0435, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0434 \u0434\u043b\u0438\u043d\u043d\u044b\u043c\u0438 \u0446\u0438\u043a\u043b\u0430\u043c\u0438. \u0418\u043d\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 <code>gc()<\/code> \u0432 \u043a\u043e\u043d\u0446\u0435 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0446\u0438\u043a\u043b\u0430.<\/p>\n<h4>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0435 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438<\/h4>\n<p>  <code>Data.table()<\/code> \u2014 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0441\u043a\u043e\u0440\u044f\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445.  <\/p>\n<pre><code class=\"python\">dt &lt;- data.table(df)  # \u0441\u043e\u0437\u0434\u0430\u0442\u044c data.table system.time({   for (i in 1:nrow (dt)) {     if ((dt[i, col1] + dt[i, col2] + dt[i, col3] + dt[i, col4]) &gt; 4) {       dt[i, col5:=&quot;greater_than_4&quot;]  # \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 5-\u0439 \u043a\u043e\u043b\u043e\u043d\u043a\u0435     } else {       dt[i, col5:=&quot;lesser_than_4&quot;]  # \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 5-\u0439 \u043a\u043e\u043b\u043e\u043d\u043a\u0435     }   } }) <\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/9f3\/bc1\/885\/9f3bc18852024e51b474f77f78e037bf.png\" width=\"60%\"\/><br \/>  <i>Dataframe \u0438 data.table<\/i><\/p>\n<h4>\u0421\u043a\u043e\u0440\u043e\u0441\u0442\u044c: \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b<\/h4>\n<p>  <b>\u041c\u0435\u0442\u043e\u0434:<\/b> \u0421\u043a\u043e\u0440\u043e\u0441\u0442\u044c, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0432 df \/ \u0437\u0430\u0442\u0440\u0430\u0447\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f = n \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<br \/>  <b>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439:<\/b> 1X, 120000\/140.15 = 856.2255 \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 (\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u043a 1)<br \/>  <b>\u0412\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439:<\/b> 738X, 120000\/0.19 = 631578.9 \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<br \/>  <b>\u0422\u043e\u043b\u044c\u043a\u043e \u0438\u0441\u0442\u0438\u043d\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f:<\/b> 1002X, 120000\/0.14 = 857142.9 \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<br \/>  <b>ifelse:<\/b> 1752X, 1200000\/0.78 = 1500000 \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<br \/>  <b>which:<\/b> 8806X, 2985984\/0.396 = 7540364 \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<br \/>  <b>Rcpp:<\/b> 13476X, 1200000\/0.09 = 11538462 \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<\/p>\n<p>  \u0427\u0438\u0441\u043b\u0430, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435, \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u044b \u0438 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u044b \u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0445. \u041d\u0435\u0442 \u0440\u0430\u0441\u0447\u0435\u0442\u043e\u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0434\u043b\u044f <code>data.table()<\/code>, \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u043e\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043a\u043e\u0434\u0430 \u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0432\u044b \u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0435.       <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habrahabr.ru\/post\/277693\/\"> https:\/\/habrahabr.ru\/post\/277693\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u0426\u0438\u043a\u043b for \u0432 R \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u043c, \u0435\u0441\u043b\u0438 \u043e\u043d \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435, \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u043c\u0435\u0442\u044c \u0434\u0435\u043b\u043e \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u043d\u0430\u0431\u043e\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u0442\u044c \u0440\u044f\u0434 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0430\u0448 \u043a\u043e\u0434 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0438 \u0432\u044b, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u0431\u0443\u0434\u0435\u0442\u0435 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u044b, \u0443\u0437\u043d\u0430\u0432 \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e.<\/p>\n<p>  \u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u043b\u043e\u0433\u0438\u043a\u0435, \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0438 <code>Rcpp<\/code>, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c 100 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u043e\u0432 \u0441\u0442\u0440\u043e\u043a \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u0431\u043e\u043b\u044c\u0448\u0435. <\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043a\u043e\u0434 \u0441 \u0446\u0438\u043a\u043b\u043e\u043c for \u0438 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c (if-else) \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u043e\u043d\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a \u043d\u0430\u0431\u043e\u0440\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 (data frame, df). \u041a\u043e\u0434 \u043d\u0438\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445.   <\/p>\n<pre><code class=\"python\"># \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 col1 &lt;- runif (12^5, 0, 2) col2 &lt;- rnorm (12^5, 0, 2) col3 &lt;- rpois (12^5, 3) col4 &lt;- rchisq (12^5, 2) df &lt;- data.frame (col1, col2, col3, col4) <\/code><\/pre>\n<p>  \u0412 <a href=\"https:\/\/habrahabr.ru\/company\/infopulse\/blog\/277681\/\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438<\/a>: \u0432\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0441\u0442\u0438\u043d\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f, ifelse.<br \/>  \u0412 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438: which, apply, \u043f\u043e\u0431\u0430\u0439\u0442\u043e\u0432\u0430\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f, Rcpp, data.table, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-275726","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/275726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=275726"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/275726\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=275726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=275726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=275726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}