{"id":198520,"date":"2023-11-01T11:24:11","date_gmt":"2023-11-01T15:24:11","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=198520"},"modified":"2023-11-01T11:24:42","modified_gmt":"2023-11-01T15:24:42","slug":"underrated-r-functions","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/","title":{"rendered":"Underrated R Functions"},"content":{"rendered":"\n<p>I wanted to write a post about a couple of handy functions in R that don\u2019t always get the recognition they deserve. This article will talk about a few functions that form part of R\u2019s core functional programming capabilities. R has thousands of functions, so this is just a short list, and I\u2019ll probably write other articles like this in the future to discuss some different R functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-reduce\"><strong>Reduce<\/strong><\/h3>\n\n\n\n<p>Let\u2019s start with the&nbsp;<strong>Reduce<\/strong>&nbsp;function (note the capital \u201cR\u201d).&nbsp;<strong>Reduce<\/strong>&nbsp;takes a list or vector as input, and reduces it down to a single element. It works by applying a function to the first two elements of the vector or list, and then applying the same function to that result with the third element. This new result gets passed with the fourth element into the function and so on until a single object remains. If the input is a vector, the result will be a single number or character. On the other hand, inputting a&nbsp;<em>list<\/em>&nbsp;can have interesting results. A list of data frames can be reduced down to a single data frame, a list of vectors can be collapsed into a matrix, and so on.<\/p>\n\n\n\n<p>A simple, though not entirely useful, example of how this works is like so:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">test &lt;- 1:10\n \nresult &lt;- Reduce(sum, test)<\/pre>\n\n\n\n<p>Here,&nbsp;<em>result<\/em>&nbsp;will equal 55, which happens to be the sum of the vector&nbsp;<em>test<\/em>&nbsp;i.e. the sum of the integers 1 through 10.&nbsp;<strong>Reduce<\/strong>&nbsp;solves for this by first applying the&nbsp;<strong>sum<\/strong>&nbsp;function to 1 and 2 (the first two elements in test). This equals 3, which then gets summed with the next element in the vector, 3. This total of 6 gets added to 4, which equals 10, and so on. The process can be seen below.<\/p>\n\n\n\n<p><strong>1 + 2 = 3<\/strong><\/p>\n\n\n\n<p><strong>3 + 3 = 6<\/strong><\/p>\n\n\n\n<p><strong>6 + 4 = 10<\/strong><\/p>\n\n\n\n<p><strong>10 + 5 = 15<\/strong><\/p>\n\n\n\n<p><strong>15 + 6 = 21<\/strong><\/p>\n\n\n\n<p><strong>21 + 7 = 28<\/strong><\/p>\n\n\n\n<p><strong>28 + 8 = 36<\/strong><\/p>\n\n\n\n<p><strong>36 + 9 = 45<\/strong><\/p>\n\n\n\n<p><strong>45 + 10 = 55<\/strong><\/p>\n\n\n\n<p>Now, how about something a little more useful? What if you had a list of vectors and you wanted to combine them into a matrix?<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">test &lt;- list(1:3, 4:6, 7:9, 10:12, 13:15, 16:18)\n \nmatrix_result &lt;- Reduce(rbind, test)<\/pre>\n\n\n\n<p>In this case, we have a list of six three-element vectors.&nbsp;<strong>Reduce<\/strong>&nbsp;applies&nbsp;<strong>rbind<\/strong>&nbsp;to the first two vectors, 1:3 and 4:6 initially. This creates a 2 x 3 matrix, where the first row is 1:3, and the second row is 4:6.<\/p>\n\n\n\n<p><strong>1 2 3<br>4 5 6<\/strong><\/p>\n\n\n\n<p>Then, the above result is combined (via&nbsp;<strong>rbind<\/strong>) to the next vector in the list, 7:9.<\/p>\n\n\n\n<p><strong>1 2 3<br>4 5 6<br>7 8 9<\/strong><\/p>\n\n\n\n<p>This process continues, as you can see below:<\/p>\n\n\n\n<p><strong>1 2 3<br>4 5 6<br>7 8 9<br>10 11 12<\/strong><\/p>\n\n\n\n<p>Next:<\/p>\n\n\n\n<p><strong>1 2 3<br>4 5 6<br>7 8 9<br>10 11 12<br>13 14 15<\/strong><\/p>\n\n\n\n<p>Finally:<\/p>\n\n\n\n<p><strong>1 2 3<br>4 5 6<br>7 8 9<br>10 11 12<br>13 14 15<br>16 17 18<\/strong><\/p>\n\n\n\n<p>Thus, the final result is a single object \u2014 but in this case, is a 6 x 3 matrix because&nbsp;<strong>rbind<\/strong>&nbsp;collapsed all of the vectors of the list, test, into a single matrix.<\/p>\n\n\n\n<p>Similarly, you could run this example using&nbsp;<strong>cbind<\/strong>&nbsp;instead of&nbsp;<strong>rbind<\/strong>&nbsp;and that would collapse the vectors column-wise, rather than row-wise.<\/p>\n\n\n\n<p>Another example where&nbsp;<strong>Reduce<\/strong>&nbsp;comes in handy might be if you want to combine a collection of data frames into a single one.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">state_data &lt;- list(FL = data.frame(state = c(\"FL\",\"FL\",\"FL\"), city = c(\"Miami\",\"Jacksonville\",\"Saint Augustine\"))\n                   NY = data.frame(state = c(\"NY\",\"NY\",\"NY\"), city = c(\"NYC\",\"Buffalo\",\"Rochester\")),\n                   MD = data.frame(state = c(\"MD\",\"MD\",\"MD\"), city = c(\"Baltimore\",\"Annapolis\",\"Ocean City\")\n                   )\n \n \ncombined &lt;- data.frame(Reduce(rbind, state_data))<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Filter<\/strong><\/h3>\n\n\n\n<p>The Filter function does basically what it sounds like \u2014 it applies a filter to a vector, list, or data frame (which is actually a type of list). It takes two main inputs, a function that applies the filter, and the object for which the filter applies.<\/p>\n\n\n\n<p>Here\u2019s a simple example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">test &lt;- 1:10\n \nless_than_5 &lt;- Filter(function(x) x &lt; 5, test)<\/pre>\n\n\n\n<p>This, once again, creates a vector of the first 10 positive integers. The&nbsp;<strong>Filter<\/strong>&nbsp;function applies&nbsp;<em>function(x) x &lt; 5<\/em>&nbsp;to each element,&nbsp;<em>x<\/em>, in the vector,&nbsp;<em>test<\/em>. In other words, it checks each element,&nbsp;<em>x<\/em>, for the Boolean expression,&nbsp;<em>x &lt; 5<\/em>. If an element is not less than 5, it gets filtered out.<\/p>\n\n\n\n<p>So you might be thinking\u2026can\u2019t this be done like this?<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">less_than_5 &lt;- test[test &lt; 5]<\/pre>\n\n\n\n<p>\u2026and the answer is\u2026yes. It can be done that way.&nbsp;<strong>Filter<\/strong>&nbsp;is more useful as a function in cases involving data frames or lists. Suppose, for instance, you want to remove all constant columns from a data frame. This is something that may be done when preprocessing data prior to modeling, as a constant attribute isn\u2019t particular useful.<\/p>\n\n\n\n<p>This is can be done in one line using&nbsp;<strong>Filter<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df &lt;- data.frame(a = c(2,2,2), b = c(1,2,3), c = c(1,1,1), d = c(3,4,5))\n \nwithout_constants &lt;- Filter(function(x) length(unique(x)) &gt; 1, df)<\/pre>\n\n\n\n<p>Alternatively, using dplyr\u2019s&nbsp;<em>n_distinct<\/em>&nbsp;function, which counts the number of distinct elements in a vector, you could do this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">library(dplyr)\n \ndf &lt;- data.frame(a = c(2,2,2), b = c(1,2,3), c = c(1,1,1), d = c(3,4,5))\n \nwithout_constants &lt;- Filter(function(x) n_distinct(x) &gt; 1, df)<\/pre>\n\n\n\n<p>In the example, we create a data frame with four columns \u2014 two of them are constant.&nbsp;<strong>Filter<\/strong>&nbsp;tests whether there is more than one unique value in each column. If there is only one unique value, then we know the column is constant, and it gets filtered out. Each element&nbsp;<em>x<\/em>&nbsp;is a vector, or column, in the data frame.<\/p>\n\n\n\n<p>If you wanted to just drop all columns that are all NAs, you could make a minor tweak like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df &lt;- data.frame(a = c(2,2,2), b = c(1,2,3), c = c(1,1,1), d = c(NA, NA, NA))\n \nwithout_nas &lt;- Filter(function(x) !all(is.na(x)), df)<\/pre>\n\n\n\n<p><strong>Filter<\/strong>&nbsp;can also be used on a regular list as well. Suppose you have a list of vectors, where some of the vectors are characters, while others are numeric. If want to filter out all of the non-numeric vectors, you could call&nbsp;<strong>Filter<\/strong>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sample_list &lt;- list(a = c(1,2,3), b = c(\"is\",\"a\",\"character\"), c = c(4,5,6), d = c(\"is\",\"another\",\"character\"))\n \nonly_numeric &lt;- Filter(function(x) is.numeric(x), sample_list)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>rapply<\/strong><\/h3>\n\n\n\n<p>The&nbsp;<strong>rapply<\/strong>&nbsp;function is part of the apply family of functions in R. It has a few different uses, but one of my favorite applications for it is to apply a function to columns of a data frame that belong to a specific class, or have a particular data type.<\/p>\n\n\n\n<p>Let\u2019s say you want to get the sum of all of the numeric columns.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df &lt;- data.frame(a = c(2,2,2), b = c(1,2,3), c = c(\"r\",\"is\",\"awesome\"), d = c(3,4,5), e=c(\"some\",\"other\",\"character\"))\n \nsummed_columns &lt;- rapply(df, sum, class = \"numeric\")<\/pre>\n\n\n\n<p>Similar to&nbsp;<em>sapply<\/em>&nbsp;or&nbsp;<em>lapply<\/em>,&nbsp;<strong>rapply<\/strong>&nbsp;takes a list \/ vector \/ data frame as input, along with a function to be applied. However, it can also take a \u201cclass\u201d parameter, which allows us to specify what class of object we want our function to be used for.<\/p>\n\n\n\n<p><strong>rapply<\/strong>&nbsp;can also be used to recursively apply functions to nested lists (see examples from its documentation&nbsp;<a href=\"https:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/base\/html\/rapply.html\">here<\/a>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>rep<\/strong><\/h3>\n\n\n\n<p>The last function I want to mention for this post is the&nbsp;<strong>rep<\/strong>&nbsp;function. This can be used to repeat a value as many times as you want. So if you want to create a vector of 1000 5\u2019s, it could be done like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">rep(5, 1000)<\/pre>\n\n\n\n<p>Here\u2019s a couple other examples:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">rep(\"a\", 500)\n \nrep(\"repeat this\", 100)<\/pre>\n\n\n\n<p>If you pass a vector with more than one element to&nbsp;<strong>rep<\/strong>, the entire vector gets repeated the number of times you specify.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">rep(c(1,2,3), 100)<\/pre>\n\n\n\n<p>The above code will create a vector with 300 elements \u2014 the number of elements in c(1,2,3) times 100, repeating 1, 2, 3 over and over.<\/p>\n\n\n\n<p><em>Originally posted on <a href=\"https:\/\/theautomatic.net\/2017\/12\/30\/underrated-r-functions\/\">TheAutomatic.net<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s start with the Reduce function (note the capital &#8220;R&#8221;). Reduce takes a list or vector as input, and reduces it down to a single element. <\/p>\n","protected":false},"author":388,"featured_media":80184,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,338,341,342],"tags":[806,2535,487,6591],"contributors-categories":[13695],"class_list":{"0":"post-198520","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-ibkr-quant-news","10":"category-quant-development","11":"category-r-development","12":"tag-data-science","13":"tag-dplyr","14":"tag-r","15":"tag-rstats","16":"contributors-categories-theautomatic-net"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Underrated R Functions | IBKR Quant<\/title>\n<meta name=\"description\" content=\"Let&#039;s start with the Reduce function (note the capital &quot;R&quot;). Reduce takes a list or vector as input, and reduces it down to a single element.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/198520\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Underrated R Functions | IBKR Campus US\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s start with the Reduce function (note the capital &quot;R&quot;). Reduce takes a list or vector as input, and reduces it down to a single element.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-01T15:24:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-01T15:24:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrew Treadway\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrew Treadway\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Andrew Treadway\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\"\n\t            },\n\t            \"headline\": \"Underrated R Functions\",\n\t            \"datePublished\": \"2023-11-01T15:24:11+00:00\",\n\t            \"dateModified\": \"2023-11-01T15:24:42+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/\"\n\t            },\n\t            \"wordCount\": 1066,\n\t            \"commentCount\": 0,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/R-programing.jpg\",\n\t            \"keywords\": [\n\t                \"Data Science\",\n\t                \"dplyr\",\n\t                \"R\",\n\t                \"rstats\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"R Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/\",\n\t            \"name\": \"Underrated R Functions | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/R-programing.jpg\",\n\t            \"datePublished\": \"2023-11-01T15:24:11+00:00\",\n\t            \"dateModified\": \"2023-11-01T15:24:42+00:00\",\n\t            \"description\": \"Let's start with the Reduce function (note the capital \\\"R\\\"). Reduce takes a list or vector as input, and reduces it down to a single element.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/underrated-r-functions\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/R-programing.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/03\\\/R-programing.jpg\",\n\t            \"width\": 900,\n\t            \"height\": 550,\n\t            \"caption\": \"R Programming\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\",\n\t            \"name\": \"Andrew Treadway\",\n\t            \"description\": \"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \\\/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\\\/\\\/www.linkedin.com\\\/in\\\/andrew-treadway-a3b19b103\\\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.\",\n\t            \"sameAs\": [\n\t                \"https:\\\/\\\/theautomatic.net\\\/about-me\\\/\"\n\t            ],\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/andrewtreadway\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Underrated R Functions | IBKR Quant","description":"Let's start with the Reduce function (note the capital \"R\"). Reduce takes a list or vector as input, and reduces it down to a single element.","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:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/198520\/","og_locale":"en_US","og_type":"article","og_title":"Underrated R Functions | IBKR Campus US","og_description":"Let's start with the Reduce function (note the capital \"R\"). Reduce takes a list or vector as input, and reduces it down to a single element.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/","og_site_name":"IBKR Campus US","article_published_time":"2023-11-01T15:24:11+00:00","article_modified_time":"2023-11-01T15:24:42+00:00","og_image":[{"width":900,"height":550,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","type":"image\/jpeg"}],"author":"Andrew Treadway","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Andrew Treadway","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/"},"author":{"name":"Andrew Treadway","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc"},"headline":"Underrated R Functions","datePublished":"2023-11-01T15:24:11+00:00","dateModified":"2023-11-01T15:24:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/"},"wordCount":1066,"commentCount":0,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","keywords":["Data Science","dplyr","R","rstats"],"articleSection":["Data Science","Programming Languages","Quant","Quant Development","R Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/","name":"Underrated R Functions | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","datePublished":"2023-11-01T15:24:11+00:00","dateModified":"2023-11-01T15:24:42+00:00","description":"Let's start with the Reduce function (note the capital \"R\"). Reduce takes a list or vector as input, and reduces it down to a single element.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/underrated-r-functions\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","width":900,"height":550,"caption":"R Programming"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc","name":"Andrew Treadway","description":"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\/\/www.linkedin.com\/in\/andrew-treadway-a3b19b103\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.","sameAs":["https:\/\/theautomatic.net\/about-me\/"],"url":"https:\/\/www.interactivebrokers.com\/campus\/author\/andrewtreadway\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/03\/R-programing.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/198520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/388"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=198520"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/198520\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/80184"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=198520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=198520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=198520"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=198520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}