{"id":896,"date":"2017-08-16T10:35:58","date_gmt":"2017-08-16T08:35:58","guid":{"rendered":"https:\/\/blog.zhaw.ch\/datascience\/?p=896"},"modified":"2018-04-09T00:08:08","modified_gmt":"2018-04-08T22:08:08","slug":"r-reduce-part-2-some-pitfalls-using-reduce","status":"publish","type":"post","link":"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/","title":{"rendered":"R: Reduce() Part 2 \u2013 some pitfalls using Reduce"},"content":{"rendered":"<p>By Matthias Templ (ZHAW), Thoralf Mildenberger (ZHAW)<\/p>\n<p>By way of example, functionality of <code>Reduce()<\/code> is shown in in <a class=\"uri\" href=\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-applys-lesser-known-brother\/\">https:\/\/blog.zhaw.ch\/datascience\/r-reduce-applys-lesser-known-brother\/<\/a> . It\u2019s great to learn about how to use this function on interesting problems. If you are ready (equals if you read the first blog post on <tt>Reduce<\/tt>), we want to push you further on writing efficient code.<!--more--><\/p>\n<p><code>Reduce()<\/code> and other base <code>R<\/code> functionality might be extremely slow compared to data manipulation functions of package <a href=\"https:\/\/cran.r-project.org\/web\/packages\/dplyr\/index.html\"> dplyr <\/a> or <a href=\"https:\/\/cran.r-project.org\/web\/packages\/data.table\/index.html\"> data.table <\/a>. In addition, compared to other software and languages, solving recursive problems with <code>R<\/code>\u2019s <code>Reduce()<\/code> is super-slow, see e.g.&nbsp;recursive problems at <a href=\"https:\/\/rosettacode.org\/wiki\/Category:R\"> Rosetta Code <\/a>, and this is sometimes used to argue that <code>R<\/code> is slow itself, which is simply not true.<\/p>\n<p>This blog post discusses some drawbacks of <code>Reduce<\/code> and base <code>R<\/code> related to user-friendliness of code and computational speed. We re-discuss two examples from <a class=\"uri\" href=\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-applys-lesser-known-brother\/\">https:\/\/blog.zhaw.ch\/datascience\/r-reduce-applys-lesser-known-brother\/<\/a><\/p>\n<div id=\"example-merging-multiple-data-sets\" class=\"section level2\">\n<h1>Example: Merging multiple data sets<\/h1>\n<p>Joining various data sets can be done with many packages, but to the best of my knowledge this could be done with package <a href=\"https:\/\/cran.r-project.org\/web\/packages\/data.table\/index.html\"> data.table <\/a>. The syntax is clear and the computational speed is just amazing.<\/p>\n<div id=\"the-former-merge-and-reduce-solutions-with-base-r\" class=\"section level4\">\n<h2>The former merge() and Reduce() solutions with base R:<\/h2>\n<pre class=\"r\"><code>df1 &lt;- data.frame(Customer_ID = c(1, 2, 3), Name = c(\"John\", \"Sue\", \"Ann\"))\r\ndf2 &lt;- data.frame(Customer_ID = c(1, 3), Year_First_Order = c(2011, 2017))\r\ndf3 &lt;- data.frame(Customer_ID = c(1, 2, 3), \r\n                  Date_Last_Order = c(\"2017-03-03\", \"2014-03-01\", \"2017-05-30\"),\r\n                  No_Items_Last_Order = c(3, 1, 1),\r\n                  Total_Amount_Last_Order = c(49, 25,25))\r\ndf4 &lt;- data.frame(Customer_ID = c(2, 3), Interested_In_Promo = c(TRUE, FALSE))<\/code><\/pre>\n<p>We could first join the first two data frames, then join the result with the third, and then join the results of that with the fourth table. In the following, a LEFT OUTER join is done using base <code>R<\/code>:<\/p>\n<pre class=\"r\"><code>df_merge &lt;- merge(df1, df2, by = \"Customer_ID\", all.x = TRUE, all.y = FALSE)\r\ndf_merge &lt;- merge(df_merge, df3, by = \"Customer_ID\", all.x = TRUE, all.y = FALSE)\r\ndf_merge &lt;- merge(df_merge, df4, by = \"Customer_ID\", all.x = TRUE, all.y = FALSE)\r\ndf_merge<\/code><\/pre>\n<pre><code>##   Customer_ID Name Year_First_Order Date_Last_Order No_Items_Last_Order\r\n## 1           1 John             2011      2017-03-03                   3\r\n## 2           2  Sue               NA      2014-03-01                   1\r\n## 3           3  Ann             2017      2017-05-30                   1\r\n##   Total_Amount_Last_Order Interested_In_Promo\r\n## 1                      49                  NA\r\n## 2                      25                TRUE\r\n## 3                      25               FALSE<\/code><\/pre>\n<p>When all the data frames are stored in one list, <code>Reduce()<\/code> can be applied in the following manner:<\/p>\n<pre class=\"r\"><code>df_list &lt;- list(df1, df2, df3, df4)\r\nReduce(function(d1, d2) merge(d1, d2, by = \"Customer_ID\", all.x = TRUE, all.y = FALSE), df_list)<\/code><\/pre>\n<pre><code>##   Customer_ID Name Year_First_Order Date_Last_Order No_Items_Last_Order\r\n## 1           1 John             2011      2017-03-03                   3\r\n## 2           2  Sue               NA      2014-03-01                   1\r\n## 3           3  Ann             2017      2017-05-30                   1\r\n##   Total_Amount_Last_Order Interested_In_Promo\r\n## 1                      49                  NA\r\n## 2                      25                TRUE\r\n## 3                      25               FALSE<\/code><\/pre>\n<p>But is this <strong>computationally efficient<\/strong>?<\/p>\n<p>We need a larger data set to get some insights.<\/p>\n<pre class=\"r\"><code>n &lt;- 300000\r\ndf1 &lt;- data.frame(Customer_ID = 1:n, Name = rep(c(\"John\", \"Sue\", \"Ann\"), n\/3))\r\ndf2 &lt;- data.frame(Customer_ID = sample(1:n, size = n*2\/3), Year_First_Order = sample(2000:2017, size = n*2\/3, replace = TRUE))\r\ndf3 &lt;- data.frame(Customer_ID = 1:n, \r\n                  Date_Last_Order = rep(c(\"2017-03-03\", \"2014-03-01\", \"2017-05-30\"),n\/3),\r\n                  No_Items_Last_Order = sample(1:10, size = n, replace = TRUE),\r\n                  Total_Amount_Last_Order = sample(20:50, size = n, replace = TRUE))\r\ndf4 &lt;- data.frame(Customer_ID = sample(1:n, size = n*2\/3), Interested_In_Promo = sample(c(TRUE, FALSE), size = n*2\/3, replace = TRUE))<\/code><\/pre>\n<p>We use the <a href=\"https:\/\/cran.r-project.org\/web\/packages\/microbenchmark\/index.html\"> microbenchmark <\/a> package to assess the computational speed. First we put our code in functions as input for <code>microbenchmark<\/code>.<\/p>\n<pre class=\"r\"><code>m1 &lt;- function(df1, df2, df3, df4){\r\n  df_merge &lt;- merge(df1, df2, by = \"Customer_ID\", all.x = TRUE, all.y = FALSE)\r\n  df_merge &lt;- merge(df_merge, df3, by = \"Customer_ID\", all.x = TRUE, all.y = FALSE)\r\n  df_merge &lt;- merge(df_merge, df4, by = \"Customer_ID\", all.x = TRUE, all.y = FALSE)\r\n  df_merge\r\n}\r\nm2 &lt;- function(df1, df2, df3, df4){\r\n  df_list &lt;- list(df1, df2, df3, df4)\r\n  Reduce(function(d1, d2) merge(d1, d2, by = \"Customer_ID\", all.x = TRUE, \r\n                                all.y = FALSE), df_list)\r\n}<\/code><\/pre>\n<pre class=\"r\"><code>library(\"microbenchmark\")\r\nmbm &lt;- microbenchmark(\r\n  mergeBase = m1(df1, df2, df3, df4),\r\n  Reduce = m2(df1, df2, df3, df4),\r\n  times = 10\r\n)\r\nmbm<\/code><\/pre>\n<pre><code>## Unit: seconds\r\n##       expr      min       lq     mean   median       uq      max neval cld\r\n##  mergeBase 1.028722 1.120588 1.126353 1.131829 1.152066 1.170740    10   a\r\n##     Reduce 1.105373 1.144046 1.168305 1.158710 1.179812 1.306395    10   a<\/code><\/pre>\n<p>About 1.15 seconds for one merge. Not bad, but if we would increase the size of our data set slightly, the computation times would increase dramatically.<\/p>\n<\/div>\n<div id=\"data.tables-solution-to-the-above-problem\" class=\"section level4\">\n<h2>data.table\u2019s solution to the above problem<\/h2>\n<p>It is relevant to ask, if <strong>more efficient code<\/strong> can be written that provides the same results. In addition, we ask the question if we can use <strong>better readable syntax<\/strong> than above?<\/p>\n<p>Let\u2019s replace our <tt>merge<\/tt> and <tt>Reduce<\/tt> solutions with <a href=\"https:\/\/cran.r-project.org\/web\/packages\/data.table\/index.html\"> data.table <\/a> code.<\/p>\n<p>We first call the package and store the data frames as data tables. In addition, we set a key for an efficient use of the binary search of the <a href=\"https:\/\/cran.r-project.org\/web\/packages\/data.table\/index.html\"> data.table <\/a> package.<\/p>\n<pre class=\"r\"><code>library(\"data.table\")\r\ndt1 &lt;- data.table(df1)\r\ndt2 &lt;- data.table(df2)\r\ndt3 &lt;- data.table(df3)\r\ndt4 &lt;- data.table(df4)\r\nsetkey(dt1, Customer_ID)\r\nsetkey(dt2, Customer_ID) \r\nsetkey(dt3, Customer_ID) \r\nsetkey(dt4, Customer_ID) <\/code><\/pre>\n<p>The following <code>data.table<\/code> code gives us exactly the same solution as with our previous base <code>R<\/code> code.<\/p>\n<p>But note, the syntax is much shorter and more user-friendly! This is all you need to write for the LEFT INNER JOIN for the given problem:<\/p>\n<pre><code>DT_cmb &lt;- dt4[dt3][dt2][dt1]<\/code><\/pre>\n<p>Lets compare the speed of all three approaches (pure merge, Reduce and data.table).<\/p>\n<pre class=\"r\"><code>m3 &lt;- function(dt1, dt2, dt3, dt4){\r\n  setkey(dt1, Customer_ID)\r\n  setkey(dt2, Customer_ID) \r\n  setkey(dt3, Customer_ID) \r\n  setkey(dt4, Customer_ID) \r\n  DT_cmb &lt;- dt4[dt3][dt2][dt1]\r\n  DT_cmb\r\n}\r\nlibrary(\"microbenchmark\")\r\nmbm &lt;- microbenchmark(\r\n  mergeBase = m1(df1, df2, df3, df4),\r\n  Reduce = m2(df1, df2, df3, df4),\r\n  dt = m3(dt1, dt2, dt3, dt4),\r\n  times = 10\r\n)\r\nmbm<\/code><\/pre>\n<pre><code>## Unit: milliseconds\r\n##       expr        min         lq       mean     median         uq      max\r\n##  mergeBase 1010.03602 1030.72226 1099.73585 1047.30145 1208.32011 1286.489\r\n##     Reduce  968.37101 1039.04347 1097.75323 1048.05562 1201.69135 1284.099\r\n##         dt   43.24658   50.42165   66.52315   53.34681   70.50837  140.710\r\n##  neval cld\r\n##     10   b\r\n##     10   b\r\n##     10  a<\/code><\/pre>\n<p>The solution with <code>data.table<\/code> is more than 15 times faster! and the code is much shorter and elegant. Note that the differences becomes even (much) larger if the data size increases.<\/p>\n<\/div>\n<div id=\"example-sums-of-matrix-powers\" class=\"section level2\">\n<h1>Example: Sums of matrix powers<\/h1>\n<div id=\"the-lapplyreduce-code\" class=\"section level4\">\n<h2>The lapply\/Reduce code:<\/h2>\n<p>We can also combine lapply() and Reduce(), such as been carried out in the following example.<\/p>\n<pre class=\"r\"><code>library(\"expm\")\r\nn &lt;- 2\r\nP &lt;- matrix(runif(n*n, 0.01, 0.02), ncol = n, nrow = n)\r\np1 &lt;- function(vec, P){\r\n  P_powers &lt;- lapply(vec, function(k) P %^% k)\r\n  Reduce(\"+\", P_powers)\r\n}\r\np1(0:2, P = P)<\/code><\/pre>\n<pre><code>##            [,1]       [,2]\r\n## [1,] 1.01718756 0.01206923\r\n## [2,] 0.01409565 1.01037824<\/code><\/pre>\n<p>What is the aim of this code? It not straightforward to see this. First different powers of matrix <strong><span class=\"math inline\">P<\/span><\/strong> are applied, then the cumulative sum of resulting matrices is calculated. We can ask if we can do it with a more intuitive and more user-friendly code (just by using a simple <tt> for <\/tt>loop), to gain computational speed and to not store the whole list of matrix powers as above.<\/p>\n<p><em>General note:<\/em> from <code>R<\/code> version 3.0.0 <tt>for<\/tt> loops are approx. equally fast than using the <code>apply<\/code> family (at least for usual problems). Multiple use of the <code>apply<\/code> family results in hard to read code, especially when several apply statements are given in one line of code, and debugging of code is more difficult.<\/p>\n<p>This is also to some extend the case with the solution above, where <tt>lapply()<\/tt> and <tt>Reduce()<\/tt> are used.<\/p>\n<\/div>\n<div id=\"the-simple-for-loop-to-the-problem\" class=\"section level4\">\n<h2>The simple for() loop to the problem:<\/h2>\n<p>The following loop just updates a matrix by adding the previous state of the matrix with the powering of the matrix.<\/p>\n<pre class=\"r\"><code>p2 &lt;- function(vec, P){\r\n  P2 &lt;- P %^% vec[1]\r\n  for(i in vec[-1]){\r\n    P2 &lt;- P2 + P %^% i\r\n  } \r\n  P2\r\n}\r\np2(0:2, P = P)<\/code><\/pre>\n<pre><code>##            [,1]       [,2]\r\n## [1,] 1.01718756 0.01206923\r\n## [2,] 0.01409565 1.01037824<\/code><\/pre>\n<p>This truly gives the same result. Let\u2019s increase the dimension of <strong><span class=\"math inline\">P<\/span><\/strong> and the number of powers applied.<\/p>\n<pre class=\"r\"><code>n &lt;- 10\r\nP &lt;- matrix(runif(n*n, 0.01, 0.02), ncol = n, nrow = n)\r\nmbm &lt;- microbenchmark(\r\n  Reduce = p1(seq_len(10000), P = P),\r\n  Simple = p2(seq_len(10000), P = P),\r\n  times = 10\r\n)\r\nmbm<\/code><\/pre>\n<pre><code>## Unit: milliseconds\r\n##    expr      min       lq     mean   median       uq      max neval cld\r\n##  Reduce 122.9000 124.2832 131.1854 133.8611 135.4301 136.7512    10   b\r\n##  Simple 109.7845 111.0879 113.7115 112.0759 113.4696 124.5798    10  a<\/code><\/pre>\n<p>The speed is comparable but slightly faster for the simple <tt>for<\/tt> loop, naturally the code is (much) better readable and debugging of code would be easier when using a <tt>for<\/tt> loop.<\/p>\n<p>Note that additional gain in computational speed can be obtained using C++ within R. This can be done, e.g.&nbsp;using package <a href=\"https:\/\/cran.r-project.org\/web\/packages\/Rcpp\/index.html\"> Rcpp <\/a>. However, in this case this is not straightforward to apply and either the environment of package <code>expm<\/code> must be imported within a <code>Rcpp<\/code> function or a powering of a matrix method must be first implemented in C++.<\/p>\n<\/div>\n<div id=\"further-reading\" class=\"section level4\">\n<h1>Further reading<\/h1>\n<p>Package <a href=\"https:\/\/cran.r-project.org\/web\/packages\/data.table\/index.html\"> data.table <\/a> is incredibly efficient in terms of computational speed. Use it whenever dealing with large (rectangular) data sets. Read its vignettes, tutorials and package manual. Note that \u2013 not used here \u2013 also package <a href=\"https:\/\/cran.r-project.org\/web\/packages\/dplyr\/index.html\"> dplyr <\/a> is fast and very user-friendly.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"pt-sm\">Schlagw\u00f6rter: <a href=\"https:\/\/blog.zhaw.ch\/datascience\/tag\/programming\/\">Programming<\/a>, <a href=\"https:\/\/blog.zhaw.ch\/datascience\/tag\/r\/\">R<\/a><br><\/div>","protected":false},"excerpt":{"rendered":"<p>By Matthias Templ (ZHAW), Thoralf Mildenberger (ZHAW) By way of example, functionality of Reduce() is shown in in https:\/\/blog.zhaw.ch\/datascience\/r-reduce-applys-lesser-known-brother\/ . It\u2019s great to learn about how to use this function on interesting problems. If you are ready (equals if you read the first blog post on Reduce), we want to push you further on writing [&hellip;]<\/p>\n","protected":false},"author":265,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1,7,42],"tags":[44,43],"features":[],"class_list":["post-896","post","type-post","status-publish","format-standard","hentry","category-allgemein","category-blog","category-r","tag-programming","tag-r"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>R: Reduce() Part 2 \u2013 some pitfalls using Reduce - Data Science made in Switzerland<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R: Reduce() Part 2 \u2013 some pitfalls using Reduce\" \/>\n<meta property=\"og:description\" content=\"By Matthias Templ (ZHAW), Thoralf Mildenberger (ZHAW) By way of example, functionality of Reduce() is shown in in https:\/\/blog.zhaw.ch\/datascience\/r-reduce-applys-lesser-known-brother\/ . It\u2019s great to learn about how to use this function on interesting problems. If you are ready (equals if you read the first blog post on Reduce), we want to push you further on writing [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/\" \/>\n<meta property=\"og:site_name\" content=\"Data Science made in Switzerland\" \/>\n<meta property=\"article:published_time\" content=\"2017-08-16T08:35:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-08T22:08:08+00:00\" \/>\n<meta name=\"author\" content=\"mild\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"mild\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/\"},\"author\":{\"name\":\"mild\",\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/#\/schema\/person\/64f2a57e0efd0aa4c73f45df76618116\"},\"headline\":\"R: Reduce() Part 2 \u2013 some pitfalls using Reduce\",\"datePublished\":\"2017-08-16T08:35:58+00:00\",\"dateModified\":\"2018-04-08T22:08:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/\"},\"wordCount\":828,\"commentCount\":0,\"keywords\":[\"Programming\",\"R\"],\"articleSection\":[\"Allgemein\",\"Blog\",\"R\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/\",\"url\":\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/\",\"name\":\"R: Reduce() Part 2 \u2013 some pitfalls using Reduce - Data Science made in Switzerland\",\"isPartOf\":{\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/#website\"},\"datePublished\":\"2017-08-16T08:35:58+00:00\",\"dateModified\":\"2018-04-08T22:08:08+00:00\",\"author\":{\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/#\/schema\/person\/64f2a57e0efd0aa4c73f45df76618116\"},\"breadcrumb\":{\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Startseite\",\"item\":\"https:\/\/blog.zhaw.ch\/datascience\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"R: Reduce() Part 2 \u2013 some pitfalls using Reduce\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/#website\",\"url\":\"https:\/\/blog.zhaw.ch\/datascience\/\",\"name\":\"Data Science made in Switzerland\",\"description\":\"Ein Blog der ZHAW Z\u00fcrcher Hochschule f\u00fcr Angewandte Wissenschaften\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.zhaw.ch\/datascience\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.zhaw.ch\/datascience\/#\/schema\/person\/64f2a57e0efd0aa4c73f45df76618116\",\"name\":\"mild\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/3c38b532abe81ed471e1e6559571ef62f075b055ca6520f8c29ee603a233e272?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3c38b532abe81ed471e1e6559571ef62f075b055ca6520f8c29ee603a233e272?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3c38b532abe81ed471e1e6559571ef62f075b055ca6520f8c29ee603a233e272?s=96&d=mm&r=g\",\"caption\":\"mild\"},\"url\":\"https:\/\/blog.zhaw.ch\/datascience\/author\/mild\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"R: Reduce() Part 2 \u2013 some pitfalls using Reduce - Data Science made in Switzerland","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/","og_locale":"en_US","og_type":"article","og_title":"R: Reduce() Part 2 \u2013 some pitfalls using Reduce","og_description":"By Matthias Templ (ZHAW), Thoralf Mildenberger (ZHAW) By way of example, functionality of Reduce() is shown in in https:\/\/blog.zhaw.ch\/datascience\/r-reduce-applys-lesser-known-brother\/ . It\u2019s great to learn about how to use this function on interesting problems. If you are ready (equals if you read the first blog post on Reduce), we want to push you further on writing [&hellip;]","og_url":"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/","og_site_name":"Data Science made in Switzerland","article_published_time":"2017-08-16T08:35:58+00:00","article_modified_time":"2018-04-08T22:08:08+00:00","author":"mild","twitter_card":"summary_large_image","twitter_misc":{"Written by":"mild","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/#article","isPartOf":{"@id":"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/"},"author":{"name":"mild","@id":"https:\/\/blog.zhaw.ch\/datascience\/#\/schema\/person\/64f2a57e0efd0aa4c73f45df76618116"},"headline":"R: Reduce() Part 2 \u2013 some pitfalls using Reduce","datePublished":"2017-08-16T08:35:58+00:00","dateModified":"2018-04-08T22:08:08+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/"},"wordCount":828,"commentCount":0,"keywords":["Programming","R"],"articleSection":["Allgemein","Blog","R"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/","url":"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/","name":"R: Reduce() Part 2 \u2013 some pitfalls using Reduce - Data Science made in Switzerland","isPartOf":{"@id":"https:\/\/blog.zhaw.ch\/datascience\/#website"},"datePublished":"2017-08-16T08:35:58+00:00","dateModified":"2018-04-08T22:08:08+00:00","author":{"@id":"https:\/\/blog.zhaw.ch\/datascience\/#\/schema\/person\/64f2a57e0efd0aa4c73f45df76618116"},"breadcrumb":{"@id":"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.zhaw.ch\/datascience\/r-reduce-part-2-some-pitfalls-using-reduce\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Startseite","item":"https:\/\/blog.zhaw.ch\/datascience\/"},{"@type":"ListItem","position":2,"name":"R: Reduce() Part 2 \u2013 some pitfalls using Reduce"}]},{"@type":"WebSite","@id":"https:\/\/blog.zhaw.ch\/datascience\/#website","url":"https:\/\/blog.zhaw.ch\/datascience\/","name":"Data Science made in Switzerland","description":"Ein Blog der ZHAW Z\u00fcrcher Hochschule f\u00fcr Angewandte Wissenschaften","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.zhaw.ch\/datascience\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.zhaw.ch\/datascience\/#\/schema\/person\/64f2a57e0efd0aa4c73f45df76618116","name":"mild","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3c38b532abe81ed471e1e6559571ef62f075b055ca6520f8c29ee603a233e272?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3c38b532abe81ed471e1e6559571ef62f075b055ca6520f8c29ee603a233e272?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3c38b532abe81ed471e1e6559571ef62f075b055ca6520f8c29ee603a233e272?s=96&d=mm&r=g","caption":"mild"},"url":"https:\/\/blog.zhaw.ch\/datascience\/author\/mild\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/posts\/896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/users\/265"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/comments?post=896"}],"version-history":[{"count":6,"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/posts\/896\/revisions"}],"predecessor-version":[{"id":936,"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/posts\/896\/revisions\/936"}],"wp:attachment":[{"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/media?parent=896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/categories?post=896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/tags?post=896"},{"taxonomy":"features","embeddable":true,"href":"https:\/\/blog.zhaw.ch\/datascience\/wp-json\/wp\/v2\/features?post=896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}